1*84e33947SAndroid Build Coastguard Worker /*
2*84e33947SAndroid Build Coastguard Worker * Copyright (C) 2020 The Android Open Source Project
3*84e33947SAndroid Build Coastguard Worker *
4*84e33947SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*84e33947SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*84e33947SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*84e33947SAndroid Build Coastguard Worker *
8*84e33947SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*84e33947SAndroid Build Coastguard Worker *
10*84e33947SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*84e33947SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*84e33947SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*84e33947SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*84e33947SAndroid Build Coastguard Worker * limitations under the License.
15*84e33947SAndroid Build Coastguard Worker */
16*84e33947SAndroid Build Coastguard Worker
17*84e33947SAndroid Build Coastguard Worker #include "chpp/services/gnss.h"
18*84e33947SAndroid Build Coastguard Worker
19*84e33947SAndroid Build Coastguard Worker #include <inttypes.h>
20*84e33947SAndroid Build Coastguard Worker #include <stddef.h>
21*84e33947SAndroid Build Coastguard Worker #include <stdint.h>
22*84e33947SAndroid Build Coastguard Worker
23*84e33947SAndroid Build Coastguard Worker #include "chpp/common/gnss.h"
24*84e33947SAndroid Build Coastguard Worker #include "chpp/common/gnss_types.h"
25*84e33947SAndroid Build Coastguard Worker #include "chpp/common/standard_uuids.h"
26*84e33947SAndroid Build Coastguard Worker #include "chpp/log.h"
27*84e33947SAndroid Build Coastguard Worker #include "chpp/macros.h"
28*84e33947SAndroid Build Coastguard Worker #include "chpp/services.h"
29*84e33947SAndroid Build Coastguard Worker #include "chre/pal/gnss.h"
30*84e33947SAndroid Build Coastguard Worker
31*84e33947SAndroid Build Coastguard Worker /************************************************
32*84e33947SAndroid Build Coastguard Worker * Prototypes
33*84e33947SAndroid Build Coastguard Worker ***********************************************/
34*84e33947SAndroid Build Coastguard Worker
35*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppDispatchGnssRequest(void *serviceContext,
36*84e33947SAndroid Build Coastguard Worker uint8_t *buf, size_t len);
37*84e33947SAndroid Build Coastguard Worker static void chppGnssServiceNotifyReset(void *serviceContext);
38*84e33947SAndroid Build Coastguard Worker
39*84e33947SAndroid Build Coastguard Worker /************************************************
40*84e33947SAndroid Build Coastguard Worker * Private Definitions
41*84e33947SAndroid Build Coastguard Worker ***********************************************/
42*84e33947SAndroid Build Coastguard Worker
43*84e33947SAndroid Build Coastguard Worker /**
44*84e33947SAndroid Build Coastguard Worker * Configuration parameters for this service
45*84e33947SAndroid Build Coastguard Worker */
46*84e33947SAndroid Build Coastguard Worker static const struct ChppService kGnssServiceConfig = {
47*84e33947SAndroid Build Coastguard Worker .descriptor.uuid = CHPP_UUID_GNSS_STANDARD,
48*84e33947SAndroid Build Coastguard Worker
49*84e33947SAndroid Build Coastguard Worker // Human-readable name
50*84e33947SAndroid Build Coastguard Worker .descriptor.name = "GNSS",
51*84e33947SAndroid Build Coastguard Worker
52*84e33947SAndroid Build Coastguard Worker // Version
53*84e33947SAndroid Build Coastguard Worker .descriptor.version.major = 1,
54*84e33947SAndroid Build Coastguard Worker .descriptor.version.minor = 0,
55*84e33947SAndroid Build Coastguard Worker .descriptor.version.patch = 0,
56*84e33947SAndroid Build Coastguard Worker
57*84e33947SAndroid Build Coastguard Worker // Notifies service if CHPP is reset
58*84e33947SAndroid Build Coastguard Worker .resetNotifierFunctionPtr = &chppGnssServiceNotifyReset,
59*84e33947SAndroid Build Coastguard Worker
60*84e33947SAndroid Build Coastguard Worker // Client request dispatch function pointer
61*84e33947SAndroid Build Coastguard Worker .requestDispatchFunctionPtr = &chppDispatchGnssRequest,
62*84e33947SAndroid Build Coastguard Worker
63*84e33947SAndroid Build Coastguard Worker // Client notification dispatch function pointer
64*84e33947SAndroid Build Coastguard Worker .notificationDispatchFunctionPtr = NULL, // Not supported
65*84e33947SAndroid Build Coastguard Worker
66*84e33947SAndroid Build Coastguard Worker // Min length is the entire header
67*84e33947SAndroid Build Coastguard Worker .minLength = sizeof(struct ChppAppHeader),
68*84e33947SAndroid Build Coastguard Worker };
69*84e33947SAndroid Build Coastguard Worker
70*84e33947SAndroid Build Coastguard Worker /**
71*84e33947SAndroid Build Coastguard Worker * Structure to maintain state for the GNSS service and its Request/Response
72*84e33947SAndroid Build Coastguard Worker * (RR) functionality.
73*84e33947SAndroid Build Coastguard Worker */
74*84e33947SAndroid Build Coastguard Worker struct ChppGnssServiceState {
75*84e33947SAndroid Build Coastguard Worker struct ChppEndpointState service; // CHPP service state
76*84e33947SAndroid Build Coastguard Worker const struct chrePalGnssApi *api; // GNSS PAL API
77*84e33947SAndroid Build Coastguard Worker
78*84e33947SAndroid Build Coastguard Worker // Based on chre/pal/gnss.h and chrePalGnssApi
79*84e33947SAndroid Build Coastguard Worker struct ChppIncomingRequestState open; // Service init state
80*84e33947SAndroid Build Coastguard Worker struct ChppIncomingRequestState close; // Service deinit state
81*84e33947SAndroid Build Coastguard Worker struct ChppIncomingRequestState getCapabilities; // Get Capabilities state
82*84e33947SAndroid Build Coastguard Worker struct ChppIncomingRequestState
83*84e33947SAndroid Build Coastguard Worker controlLocationSession; // Control Location measurement state
84*84e33947SAndroid Build Coastguard Worker struct ChppIncomingRequestState
85*84e33947SAndroid Build Coastguard Worker controlMeasurementSession; // Control Raw GNSS measurement state
86*84e33947SAndroid Build Coastguard Worker struct ChppIncomingRequestState
87*84e33947SAndroid Build Coastguard Worker configurePassiveLocationListener; // Configure Passive location receiving
88*84e33947SAndroid Build Coastguard Worker // state
89*84e33947SAndroid Build Coastguard Worker };
90*84e33947SAndroid Build Coastguard Worker
91*84e33947SAndroid Build Coastguard Worker // Note: The CHRE PAL API only allows for one definition - see comment in WWAN
92*84e33947SAndroid Build Coastguard Worker // service for details.
93*84e33947SAndroid Build Coastguard Worker // Note: There is no notion of a cookie in the CHRE GNSS API so we need to use
94*84e33947SAndroid Build Coastguard Worker // the global service state (gGnssServiceContext) directly in all callbacks.
95*84e33947SAndroid Build Coastguard Worker struct ChppGnssServiceState gGnssServiceContext;
96*84e33947SAndroid Build Coastguard Worker
97*84e33947SAndroid Build Coastguard Worker /************************************************
98*84e33947SAndroid Build Coastguard Worker * Prototypes
99*84e33947SAndroid Build Coastguard Worker ***********************************************/
100*84e33947SAndroid Build Coastguard Worker
101*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppGnssServiceOpen(
102*84e33947SAndroid Build Coastguard Worker struct ChppGnssServiceState *gnssServiceContext,
103*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader);
104*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppGnssServiceClose(
105*84e33947SAndroid Build Coastguard Worker struct ChppGnssServiceState *gnssServiceContext,
106*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader);
107*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppGnssServiceGetCapabilities(
108*84e33947SAndroid Build Coastguard Worker struct ChppGnssServiceState *gnssServiceContext,
109*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader);
110*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppGnssServiceControlLocationSession(
111*84e33947SAndroid Build Coastguard Worker struct ChppGnssServiceState *gnssServiceContext,
112*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader, uint8_t *buf, size_t len);
113*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppGnssServiceControlMeasurementSession(
114*84e33947SAndroid Build Coastguard Worker struct ChppGnssServiceState *gnssServiceContext,
115*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader, uint8_t *buf, size_t len);
116*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppGnssServiceConfigurePassiveLocationListener(
117*84e33947SAndroid Build Coastguard Worker struct ChppGnssServiceState *gnssServiceContext,
118*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader, uint8_t *buf, size_t len);
119*84e33947SAndroid Build Coastguard Worker
120*84e33947SAndroid Build Coastguard Worker static void chppGnssServiceRequestStateResyncCallback(void);
121*84e33947SAndroid Build Coastguard Worker static void chppGnssServiceLocationStatusChangeCallback(bool enabled,
122*84e33947SAndroid Build Coastguard Worker uint8_t errorCode);
123*84e33947SAndroid Build Coastguard Worker static void chppGnssServiceLocationEventCallback(
124*84e33947SAndroid Build Coastguard Worker struct chreGnssLocationEvent *event);
125*84e33947SAndroid Build Coastguard Worker static void chppGnssServiceMeasurementStatusChangeCallback(bool enabled,
126*84e33947SAndroid Build Coastguard Worker uint8_t errorCode);
127*84e33947SAndroid Build Coastguard Worker static void chppGnssServiceMeasurementEventCallback(
128*84e33947SAndroid Build Coastguard Worker struct chreGnssDataEvent *event);
129*84e33947SAndroid Build Coastguard Worker
130*84e33947SAndroid Build Coastguard Worker /************************************************
131*84e33947SAndroid Build Coastguard Worker * Private Functions
132*84e33947SAndroid Build Coastguard Worker ***********************************************/
133*84e33947SAndroid Build Coastguard Worker
134*84e33947SAndroid Build Coastguard Worker /**
135*84e33947SAndroid Build Coastguard Worker * Dispatches a client request from the transport layer that is determined to be
136*84e33947SAndroid Build Coastguard Worker * for the GNSS service. If the result of the dispatch is an error, this
137*84e33947SAndroid Build Coastguard Worker * function responds to the client with the same error.
138*84e33947SAndroid Build Coastguard Worker *
139*84e33947SAndroid Build Coastguard Worker * This function is called from the app layer using its function pointer given
140*84e33947SAndroid Build Coastguard Worker * during service registration.
141*84e33947SAndroid Build Coastguard Worker *
142*84e33947SAndroid Build Coastguard Worker * @param serviceContext Maintains status for each service instance.
143*84e33947SAndroid Build Coastguard Worker * @param buf Input data. Cannot be null.
144*84e33947SAndroid Build Coastguard Worker * @param len Length of input data in bytes.
145*84e33947SAndroid Build Coastguard Worker *
146*84e33947SAndroid Build Coastguard Worker * @return Indicates the result of this function call.
147*84e33947SAndroid Build Coastguard Worker */
chppDispatchGnssRequest(void * serviceContext,uint8_t * buf,size_t len)148*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppDispatchGnssRequest(void *serviceContext,
149*84e33947SAndroid Build Coastguard Worker uint8_t *buf, size_t len) {
150*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *rxHeader = (struct ChppAppHeader *)buf;
151*84e33947SAndroid Build Coastguard Worker buf += sizeof(struct ChppAppHeader);
152*84e33947SAndroid Build Coastguard Worker len -= sizeof(struct ChppAppHeader);
153*84e33947SAndroid Build Coastguard Worker
154*84e33947SAndroid Build Coastguard Worker struct ChppGnssServiceState *gnssServiceContext =
155*84e33947SAndroid Build Coastguard Worker (struct ChppGnssServiceState *)serviceContext;
156*84e33947SAndroid Build Coastguard Worker struct ChppIncomingRequestState *inReqState = NULL;
157*84e33947SAndroid Build Coastguard Worker enum ChppAppErrorCode error = CHPP_APP_ERROR_NONE;
158*84e33947SAndroid Build Coastguard Worker bool dispatched = true;
159*84e33947SAndroid Build Coastguard Worker
160*84e33947SAndroid Build Coastguard Worker switch (rxHeader->command) {
161*84e33947SAndroid Build Coastguard Worker case CHPP_GNSS_OPEN: {
162*84e33947SAndroid Build Coastguard Worker inReqState = &gnssServiceContext->open;
163*84e33947SAndroid Build Coastguard Worker chppTimestampIncomingRequest(inReqState, rxHeader);
164*84e33947SAndroid Build Coastguard Worker error = chppGnssServiceOpen(gnssServiceContext, rxHeader);
165*84e33947SAndroid Build Coastguard Worker break;
166*84e33947SAndroid Build Coastguard Worker }
167*84e33947SAndroid Build Coastguard Worker
168*84e33947SAndroid Build Coastguard Worker case CHPP_GNSS_CLOSE: {
169*84e33947SAndroid Build Coastguard Worker inReqState = &gnssServiceContext->close;
170*84e33947SAndroid Build Coastguard Worker chppTimestampIncomingRequest(inReqState, rxHeader);
171*84e33947SAndroid Build Coastguard Worker error = chppGnssServiceClose(gnssServiceContext, rxHeader);
172*84e33947SAndroid Build Coastguard Worker break;
173*84e33947SAndroid Build Coastguard Worker }
174*84e33947SAndroid Build Coastguard Worker
175*84e33947SAndroid Build Coastguard Worker case CHPP_GNSS_GET_CAPABILITIES: {
176*84e33947SAndroid Build Coastguard Worker inReqState = &gnssServiceContext->getCapabilities;
177*84e33947SAndroid Build Coastguard Worker chppTimestampIncomingRequest(inReqState, rxHeader);
178*84e33947SAndroid Build Coastguard Worker error = chppGnssServiceGetCapabilities(gnssServiceContext, rxHeader);
179*84e33947SAndroid Build Coastguard Worker break;
180*84e33947SAndroid Build Coastguard Worker }
181*84e33947SAndroid Build Coastguard Worker
182*84e33947SAndroid Build Coastguard Worker case CHPP_GNSS_CONTROL_LOCATION_SESSION: {
183*84e33947SAndroid Build Coastguard Worker inReqState = &gnssServiceContext->controlLocationSession;
184*84e33947SAndroid Build Coastguard Worker chppTimestampIncomingRequest(inReqState, rxHeader);
185*84e33947SAndroid Build Coastguard Worker error = chppGnssServiceControlLocationSession(gnssServiceContext,
186*84e33947SAndroid Build Coastguard Worker rxHeader, buf, len);
187*84e33947SAndroid Build Coastguard Worker break;
188*84e33947SAndroid Build Coastguard Worker }
189*84e33947SAndroid Build Coastguard Worker
190*84e33947SAndroid Build Coastguard Worker case CHPP_GNSS_CONTROL_MEASUREMENT_SESSION: {
191*84e33947SAndroid Build Coastguard Worker inReqState = &gnssServiceContext->controlMeasurementSession;
192*84e33947SAndroid Build Coastguard Worker chppTimestampIncomingRequest(inReqState, rxHeader);
193*84e33947SAndroid Build Coastguard Worker error = chppGnssServiceControlMeasurementSession(gnssServiceContext,
194*84e33947SAndroid Build Coastguard Worker rxHeader, buf, len);
195*84e33947SAndroid Build Coastguard Worker break;
196*84e33947SAndroid Build Coastguard Worker }
197*84e33947SAndroid Build Coastguard Worker
198*84e33947SAndroid Build Coastguard Worker case CHPP_GNSS_CONFIGURE_PASSIVE_LOCATION_LISTENER: {
199*84e33947SAndroid Build Coastguard Worker inReqState = &gnssServiceContext->configurePassiveLocationListener;
200*84e33947SAndroid Build Coastguard Worker chppTimestampIncomingRequest(inReqState, rxHeader);
201*84e33947SAndroid Build Coastguard Worker error = chppGnssServiceConfigurePassiveLocationListener(
202*84e33947SAndroid Build Coastguard Worker gnssServiceContext, rxHeader, buf, len);
203*84e33947SAndroid Build Coastguard Worker break;
204*84e33947SAndroid Build Coastguard Worker }
205*84e33947SAndroid Build Coastguard Worker
206*84e33947SAndroid Build Coastguard Worker default: {
207*84e33947SAndroid Build Coastguard Worker dispatched = false;
208*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_INVALID_COMMAND;
209*84e33947SAndroid Build Coastguard Worker break;
210*84e33947SAndroid Build Coastguard Worker }
211*84e33947SAndroid Build Coastguard Worker }
212*84e33947SAndroid Build Coastguard Worker
213*84e33947SAndroid Build Coastguard Worker if (dispatched == true && error != CHPP_APP_ERROR_NONE) {
214*84e33947SAndroid Build Coastguard Worker // Request was dispatched but an error was returned. Close out
215*84e33947SAndroid Build Coastguard Worker // chppTimestampIncomingRequest()
216*84e33947SAndroid Build Coastguard Worker chppTimestampOutgoingResponse(inReqState);
217*84e33947SAndroid Build Coastguard Worker }
218*84e33947SAndroid Build Coastguard Worker
219*84e33947SAndroid Build Coastguard Worker return error;
220*84e33947SAndroid Build Coastguard Worker }
221*84e33947SAndroid Build Coastguard Worker
222*84e33947SAndroid Build Coastguard Worker /**
223*84e33947SAndroid Build Coastguard Worker * Initializes the GNSS service upon an open request from the client and
224*84e33947SAndroid Build Coastguard Worker * responds to the client with the result.
225*84e33947SAndroid Build Coastguard Worker *
226*84e33947SAndroid Build Coastguard Worker * @param serviceContext Maintains status for each service instance.
227*84e33947SAndroid Build Coastguard Worker * @param requestHeader App layer header of the request.
228*84e33947SAndroid Build Coastguard Worker *
229*84e33947SAndroid Build Coastguard Worker * @return Indicates the result of this function call.
230*84e33947SAndroid Build Coastguard Worker */
chppGnssServiceOpen(struct ChppGnssServiceState * gnssServiceContext,struct ChppAppHeader * requestHeader)231*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppGnssServiceOpen(
232*84e33947SAndroid Build Coastguard Worker struct ChppGnssServiceState *gnssServiceContext,
233*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader) {
234*84e33947SAndroid Build Coastguard Worker static const struct chrePalGnssCallbacks palCallbacks = {
235*84e33947SAndroid Build Coastguard Worker .requestStateResync = chppGnssServiceRequestStateResyncCallback,
236*84e33947SAndroid Build Coastguard Worker .locationStatusChangeCallback =
237*84e33947SAndroid Build Coastguard Worker chppGnssServiceLocationStatusChangeCallback,
238*84e33947SAndroid Build Coastguard Worker .locationEventCallback = chppGnssServiceLocationEventCallback,
239*84e33947SAndroid Build Coastguard Worker .measurementStatusChangeCallback =
240*84e33947SAndroid Build Coastguard Worker chppGnssServiceMeasurementStatusChangeCallback,
241*84e33947SAndroid Build Coastguard Worker .measurementEventCallback = chppGnssServiceMeasurementEventCallback,
242*84e33947SAndroid Build Coastguard Worker };
243*84e33947SAndroid Build Coastguard Worker
244*84e33947SAndroid Build Coastguard Worker enum ChppAppErrorCode error = CHPP_APP_ERROR_NONE;
245*84e33947SAndroid Build Coastguard Worker
246*84e33947SAndroid Build Coastguard Worker if (gnssServiceContext->service.openState == CHPP_OPEN_STATE_OPENED) {
247*84e33947SAndroid Build Coastguard Worker CHPP_DEBUG_ASSERT_LOG(false, "GNSS service already open");
248*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_INVALID_COMMAND;
249*84e33947SAndroid Build Coastguard Worker
250*84e33947SAndroid Build Coastguard Worker } else if (!gnssServiceContext->api->open(
251*84e33947SAndroid Build Coastguard Worker gnssServiceContext->service.appContext->systemApi,
252*84e33947SAndroid Build Coastguard Worker &palCallbacks)) {
253*84e33947SAndroid Build Coastguard Worker CHPP_DEBUG_ASSERT_LOG(false, "GNSS PAL open failed");
254*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_BEYOND_CHPP;
255*84e33947SAndroid Build Coastguard Worker
256*84e33947SAndroid Build Coastguard Worker } else {
257*84e33947SAndroid Build Coastguard Worker CHPP_LOGD("GNSS service opened");
258*84e33947SAndroid Build Coastguard Worker gnssServiceContext->service.openState = CHPP_OPEN_STATE_OPENED;
259*84e33947SAndroid Build Coastguard Worker
260*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *response =
261*84e33947SAndroid Build Coastguard Worker chppAllocResponseFixed(requestHeader, struct ChppAppHeader);
262*84e33947SAndroid Build Coastguard Worker size_t responseLen = sizeof(*response);
263*84e33947SAndroid Build Coastguard Worker
264*84e33947SAndroid Build Coastguard Worker if (response == NULL) {
265*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
266*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_OOM;
267*84e33947SAndroid Build Coastguard Worker } else {
268*84e33947SAndroid Build Coastguard Worker chppSendTimestampedResponseOrFail(gnssServiceContext->service.appContext,
269*84e33947SAndroid Build Coastguard Worker &gnssServiceContext->open, response,
270*84e33947SAndroid Build Coastguard Worker responseLen);
271*84e33947SAndroid Build Coastguard Worker }
272*84e33947SAndroid Build Coastguard Worker }
273*84e33947SAndroid Build Coastguard Worker
274*84e33947SAndroid Build Coastguard Worker return error;
275*84e33947SAndroid Build Coastguard Worker }
276*84e33947SAndroid Build Coastguard Worker
277*84e33947SAndroid Build Coastguard Worker /**
278*84e33947SAndroid Build Coastguard Worker * Deinitializes the GNSS service.
279*84e33947SAndroid Build Coastguard Worker *
280*84e33947SAndroid Build Coastguard Worker * @param serviceContext Maintains status for each service instance.
281*84e33947SAndroid Build Coastguard Worker * @param requestHeader App layer header of the request.
282*84e33947SAndroid Build Coastguard Worker *
283*84e33947SAndroid Build Coastguard Worker * @return Indicates the result of this function call.
284*84e33947SAndroid Build Coastguard Worker */
chppGnssServiceClose(struct ChppGnssServiceState * gnssServiceContext,struct ChppAppHeader * requestHeader)285*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppGnssServiceClose(
286*84e33947SAndroid Build Coastguard Worker struct ChppGnssServiceState *gnssServiceContext,
287*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader) {
288*84e33947SAndroid Build Coastguard Worker enum ChppAppErrorCode error = CHPP_APP_ERROR_NONE;
289*84e33947SAndroid Build Coastguard Worker
290*84e33947SAndroid Build Coastguard Worker gnssServiceContext->api->close();
291*84e33947SAndroid Build Coastguard Worker gnssServiceContext->service.openState = CHPP_OPEN_STATE_CLOSED;
292*84e33947SAndroid Build Coastguard Worker
293*84e33947SAndroid Build Coastguard Worker CHPP_LOGD("GNSS service closed");
294*84e33947SAndroid Build Coastguard Worker
295*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *response =
296*84e33947SAndroid Build Coastguard Worker chppAllocResponseFixed(requestHeader, struct ChppAppHeader);
297*84e33947SAndroid Build Coastguard Worker size_t responseLen = sizeof(*response);
298*84e33947SAndroid Build Coastguard Worker
299*84e33947SAndroid Build Coastguard Worker if (response == NULL) {
300*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
301*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_OOM;
302*84e33947SAndroid Build Coastguard Worker } else {
303*84e33947SAndroid Build Coastguard Worker chppSendTimestampedResponseOrFail(gnssServiceContext->service.appContext,
304*84e33947SAndroid Build Coastguard Worker &gnssServiceContext->close, response,
305*84e33947SAndroid Build Coastguard Worker responseLen);
306*84e33947SAndroid Build Coastguard Worker }
307*84e33947SAndroid Build Coastguard Worker
308*84e33947SAndroid Build Coastguard Worker return error;
309*84e33947SAndroid Build Coastguard Worker }
310*84e33947SAndroid Build Coastguard Worker
311*84e33947SAndroid Build Coastguard Worker /**
312*84e33947SAndroid Build Coastguard Worker * Notifies the service of an incoming reset.
313*84e33947SAndroid Build Coastguard Worker *
314*84e33947SAndroid Build Coastguard Worker * @param serviceContext Maintains status for each service instance.
315*84e33947SAndroid Build Coastguard Worker */
chppGnssServiceNotifyReset(void * serviceContext)316*84e33947SAndroid Build Coastguard Worker static void chppGnssServiceNotifyReset(void *serviceContext) {
317*84e33947SAndroid Build Coastguard Worker struct ChppGnssServiceState *gnssServiceContext =
318*84e33947SAndroid Build Coastguard Worker (struct ChppGnssServiceState *)serviceContext;
319*84e33947SAndroid Build Coastguard Worker
320*84e33947SAndroid Build Coastguard Worker if (gnssServiceContext->service.openState != CHPP_OPEN_STATE_OPENED) {
321*84e33947SAndroid Build Coastguard Worker CHPP_LOGW("GNSS service reset but wasn't open");
322*84e33947SAndroid Build Coastguard Worker } else {
323*84e33947SAndroid Build Coastguard Worker CHPP_LOGD("GNSS service reset. Closing");
324*84e33947SAndroid Build Coastguard Worker gnssServiceContext->service.openState = CHPP_OPEN_STATE_CLOSED;
325*84e33947SAndroid Build Coastguard Worker gnssServiceContext->api->close();
326*84e33947SAndroid Build Coastguard Worker }
327*84e33947SAndroid Build Coastguard Worker }
328*84e33947SAndroid Build Coastguard Worker
329*84e33947SAndroid Build Coastguard Worker /**
330*84e33947SAndroid Build Coastguard Worker * Retrieves a set of flags indicating the GNSS features supported by the
331*84e33947SAndroid Build Coastguard Worker * current implementation.
332*84e33947SAndroid Build Coastguard Worker *
333*84e33947SAndroid Build Coastguard Worker * @param serviceContext Maintains status for each service instance.
334*84e33947SAndroid Build Coastguard Worker * @param requestHeader App layer header of the request.
335*84e33947SAndroid Build Coastguard Worker *
336*84e33947SAndroid Build Coastguard Worker * @return Indicates the result of this function call.
337*84e33947SAndroid Build Coastguard Worker */
chppGnssServiceGetCapabilities(struct ChppGnssServiceState * gnssServiceContext,struct ChppAppHeader * requestHeader)338*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppGnssServiceGetCapabilities(
339*84e33947SAndroid Build Coastguard Worker struct ChppGnssServiceState *gnssServiceContext,
340*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader) {
341*84e33947SAndroid Build Coastguard Worker enum ChppAppErrorCode error = CHPP_APP_ERROR_NONE;
342*84e33947SAndroid Build Coastguard Worker
343*84e33947SAndroid Build Coastguard Worker struct ChppGnssGetCapabilitiesResponse *response = chppAllocResponseFixed(
344*84e33947SAndroid Build Coastguard Worker requestHeader, struct ChppGnssGetCapabilitiesResponse);
345*84e33947SAndroid Build Coastguard Worker size_t responseLen = sizeof(*response);
346*84e33947SAndroid Build Coastguard Worker
347*84e33947SAndroid Build Coastguard Worker if (response == NULL) {
348*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
349*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_OOM;
350*84e33947SAndroid Build Coastguard Worker } else {
351*84e33947SAndroid Build Coastguard Worker response->params.capabilities = gnssServiceContext->api->getCapabilities();
352*84e33947SAndroid Build Coastguard Worker
353*84e33947SAndroid Build Coastguard Worker CHPP_LOGD("chppGnssServiceGetCapabilities returning 0x%" PRIx32
354*84e33947SAndroid Build Coastguard Worker ", %" PRIuSIZE " bytes",
355*84e33947SAndroid Build Coastguard Worker response->params.capabilities, responseLen);
356*84e33947SAndroid Build Coastguard Worker chppSendTimestampedResponseOrFail(gnssServiceContext->service.appContext,
357*84e33947SAndroid Build Coastguard Worker &gnssServiceContext->getCapabilities,
358*84e33947SAndroid Build Coastguard Worker response, responseLen);
359*84e33947SAndroid Build Coastguard Worker }
360*84e33947SAndroid Build Coastguard Worker
361*84e33947SAndroid Build Coastguard Worker return error;
362*84e33947SAndroid Build Coastguard Worker }
363*84e33947SAndroid Build Coastguard Worker
364*84e33947SAndroid Build Coastguard Worker /**
365*84e33947SAndroid Build Coastguard Worker * Start/stop/modify the GNSS location session.
366*84e33947SAndroid Build Coastguard Worker *
367*84e33947SAndroid Build Coastguard Worker * This function returns an error code synchronously.
368*84e33947SAndroid Build Coastguard Worker * A subsequent call to chppGnssServiceLocationStatusChangeCallback() will be
369*84e33947SAndroid Build Coastguard Worker * used to communicate the result of this request (as a service response).
370*84e33947SAndroid Build Coastguard Worker * A subsequent call to chppGnssServiceLocationEventCallback() will be used to
371*84e33947SAndroid Build Coastguard Worker * communicate the location fixes (as service notifications).
372*84e33947SAndroid Build Coastguard Worker *
373*84e33947SAndroid Build Coastguard Worker * @param serviceContext Maintains status for each service instance.
374*84e33947SAndroid Build Coastguard Worker * @param requestHeader App layer header of the request.
375*84e33947SAndroid Build Coastguard Worker * @param buf Input data. Cannot be null.
376*84e33947SAndroid Build Coastguard Worker * @param len Length of input data in bytes.
377*84e33947SAndroid Build Coastguard Worker *
378*84e33947SAndroid Build Coastguard Worker * @return Indicates the result of this function call.
379*84e33947SAndroid Build Coastguard Worker */
chppGnssServiceControlLocationSession(struct ChppGnssServiceState * gnssServiceContext,struct ChppAppHeader * requestHeader,uint8_t * buf,size_t len)380*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppGnssServiceControlLocationSession(
381*84e33947SAndroid Build Coastguard Worker struct ChppGnssServiceState *gnssServiceContext,
382*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader, uint8_t *buf, size_t len) {
383*84e33947SAndroid Build Coastguard Worker UNUSED_VAR(requestHeader);
384*84e33947SAndroid Build Coastguard Worker enum ChppAppErrorCode error = CHPP_APP_ERROR_NONE;
385*84e33947SAndroid Build Coastguard Worker
386*84e33947SAndroid Build Coastguard Worker if (len < sizeof(struct ChppGnssControlLocationSessionParameters)) {
387*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_INVALID_ARG;
388*84e33947SAndroid Build Coastguard Worker
389*84e33947SAndroid Build Coastguard Worker } else {
390*84e33947SAndroid Build Coastguard Worker struct ChppGnssControlLocationSessionParameters *parameters =
391*84e33947SAndroid Build Coastguard Worker (struct ChppGnssControlLocationSessionParameters *)buf;
392*84e33947SAndroid Build Coastguard Worker
393*84e33947SAndroid Build Coastguard Worker if (!gnssServiceContext->api->controlLocationSession(
394*84e33947SAndroid Build Coastguard Worker parameters->enable, parameters->minIntervalMs,
395*84e33947SAndroid Build Coastguard Worker parameters->minTimeToNextFixMs)) {
396*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_UNSPECIFIED;
397*84e33947SAndroid Build Coastguard Worker }
398*84e33947SAndroid Build Coastguard Worker }
399*84e33947SAndroid Build Coastguard Worker
400*84e33947SAndroid Build Coastguard Worker return error;
401*84e33947SAndroid Build Coastguard Worker }
402*84e33947SAndroid Build Coastguard Worker
403*84e33947SAndroid Build Coastguard Worker /**
404*84e33947SAndroid Build Coastguard Worker * Start/stop/modify the raw GNSS measurement session.
405*84e33947SAndroid Build Coastguard Worker *
406*84e33947SAndroid Build Coastguard Worker * This function returns an error code synchronously.
407*84e33947SAndroid Build Coastguard Worker * A subsequent call to chppGnssServiceMeasurementStatusChangeCallback() will be
408*84e33947SAndroid Build Coastguard Worker * used to communicate the result of this request (as a service response).
409*84e33947SAndroid Build Coastguard Worker * A subsequent call to chppGnssServiceMeasurementEventCallback() will be used
410*84e33947SAndroid Build Coastguard Worker * to communicate the measurements (as service notifications).
411*84e33947SAndroid Build Coastguard Worker *
412*84e33947SAndroid Build Coastguard Worker * @param serviceContext Maintains status for each service instance.
413*84e33947SAndroid Build Coastguard Worker * @param requestHeader App layer header of the request.
414*84e33947SAndroid Build Coastguard Worker * @param buf Input data. Cannot be null.
415*84e33947SAndroid Build Coastguard Worker * @param len Length of input data in bytes.
416*84e33947SAndroid Build Coastguard Worker *
417*84e33947SAndroid Build Coastguard Worker * @return Indicates the result of this function call.
418*84e33947SAndroid Build Coastguard Worker */
chppGnssServiceControlMeasurementSession(struct ChppGnssServiceState * gnssServiceContext,struct ChppAppHeader * requestHeader,uint8_t * buf,size_t len)419*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppGnssServiceControlMeasurementSession(
420*84e33947SAndroid Build Coastguard Worker struct ChppGnssServiceState *gnssServiceContext,
421*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader, uint8_t *buf, size_t len) {
422*84e33947SAndroid Build Coastguard Worker UNUSED_VAR(requestHeader);
423*84e33947SAndroid Build Coastguard Worker enum ChppAppErrorCode error = CHPP_APP_ERROR_NONE;
424*84e33947SAndroid Build Coastguard Worker
425*84e33947SAndroid Build Coastguard Worker if (len < sizeof(struct ChppGnssControlMeasurementSessionParameters)) {
426*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_INVALID_ARG;
427*84e33947SAndroid Build Coastguard Worker
428*84e33947SAndroid Build Coastguard Worker } else {
429*84e33947SAndroid Build Coastguard Worker struct ChppGnssControlMeasurementSessionParameters *parameters =
430*84e33947SAndroid Build Coastguard Worker (struct ChppGnssControlMeasurementSessionParameters *)buf;
431*84e33947SAndroid Build Coastguard Worker
432*84e33947SAndroid Build Coastguard Worker if (!gnssServiceContext->api->controlMeasurementSession(
433*84e33947SAndroid Build Coastguard Worker parameters->enable, parameters->minIntervalMs)) {
434*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_UNSPECIFIED;
435*84e33947SAndroid Build Coastguard Worker }
436*84e33947SAndroid Build Coastguard Worker }
437*84e33947SAndroid Build Coastguard Worker
438*84e33947SAndroid Build Coastguard Worker return error;
439*84e33947SAndroid Build Coastguard Worker }
440*84e33947SAndroid Build Coastguard Worker
441*84e33947SAndroid Build Coastguard Worker /**
442*84e33947SAndroid Build Coastguard Worker * Configures whether to opportunistically deliver any location fixes produced
443*84e33947SAndroid Build Coastguard Worker * for other clients of the GNSS engine.
444*84e33947SAndroid Build Coastguard Worker *
445*84e33947SAndroid Build Coastguard Worker * This function returns an error code synchronously.
446*84e33947SAndroid Build Coastguard Worker * A subsequent call to chppGnssServiceLocationEventCallback() will be used to
447*84e33947SAndroid Build Coastguard Worker * communicate the location fixes (as service notifications).
448*84e33947SAndroid Build Coastguard Worker *
449*84e33947SAndroid Build Coastguard Worker * @param serviceContext Maintains status for each service instance.
450*84e33947SAndroid Build Coastguard Worker * @param requestHeader App layer header of the request.
451*84e33947SAndroid Build Coastguard Worker * @param buf Input data. Cannot be null.
452*84e33947SAndroid Build Coastguard Worker * @param len Length of input data in bytes.
453*84e33947SAndroid Build Coastguard Worker *
454*84e33947SAndroid Build Coastguard Worker * @return Indicates the result of this function call.
455*84e33947SAndroid Build Coastguard Worker */
chppGnssServiceConfigurePassiveLocationListener(struct ChppGnssServiceState * gnssServiceContext,struct ChppAppHeader * requestHeader,uint8_t * buf,size_t len)456*84e33947SAndroid Build Coastguard Worker static enum ChppAppErrorCode chppGnssServiceConfigurePassiveLocationListener(
457*84e33947SAndroid Build Coastguard Worker struct ChppGnssServiceState *gnssServiceContext,
458*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *requestHeader, uint8_t *buf, size_t len) {
459*84e33947SAndroid Build Coastguard Worker UNUSED_VAR(requestHeader);
460*84e33947SAndroid Build Coastguard Worker enum ChppAppErrorCode error = CHPP_APP_ERROR_NONE;
461*84e33947SAndroid Build Coastguard Worker
462*84e33947SAndroid Build Coastguard Worker if (len < sizeof(struct ChppGnssConfigurePassiveLocationListenerParameters)) {
463*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_INVALID_ARG;
464*84e33947SAndroid Build Coastguard Worker } else {
465*84e33947SAndroid Build Coastguard Worker struct ChppGnssConfigurePassiveLocationListenerParameters *parameters =
466*84e33947SAndroid Build Coastguard Worker (struct ChppGnssConfigurePassiveLocationListenerParameters *)buf;
467*84e33947SAndroid Build Coastguard Worker
468*84e33947SAndroid Build Coastguard Worker if (!gnssServiceContext->api->configurePassiveLocationListener(
469*84e33947SAndroid Build Coastguard Worker parameters->enable)) {
470*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_UNSPECIFIED;
471*84e33947SAndroid Build Coastguard Worker
472*84e33947SAndroid Build Coastguard Worker } else {
473*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *response =
474*84e33947SAndroid Build Coastguard Worker chppAllocResponseFixed(requestHeader, struct ChppAppHeader);
475*84e33947SAndroid Build Coastguard Worker size_t responseLen = sizeof(*response);
476*84e33947SAndroid Build Coastguard Worker
477*84e33947SAndroid Build Coastguard Worker if (response == NULL) {
478*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
479*84e33947SAndroid Build Coastguard Worker error = CHPP_APP_ERROR_OOM;
480*84e33947SAndroid Build Coastguard Worker } else {
481*84e33947SAndroid Build Coastguard Worker chppSendTimestampedResponseOrFail(
482*84e33947SAndroid Build Coastguard Worker gnssServiceContext->service.appContext,
483*84e33947SAndroid Build Coastguard Worker &gnssServiceContext->configurePassiveLocationListener, response,
484*84e33947SAndroid Build Coastguard Worker responseLen);
485*84e33947SAndroid Build Coastguard Worker }
486*84e33947SAndroid Build Coastguard Worker }
487*84e33947SAndroid Build Coastguard Worker }
488*84e33947SAndroid Build Coastguard Worker
489*84e33947SAndroid Build Coastguard Worker return error;
490*84e33947SAndroid Build Coastguard Worker }
491*84e33947SAndroid Build Coastguard Worker
492*84e33947SAndroid Build Coastguard Worker /**
493*84e33947SAndroid Build Coastguard Worker * GNSS PAL callback to request that the core CHRE system re-send requests for
494*84e33947SAndroid Build Coastguard Worker * any active sessions and its current passive location listener setting.
495*84e33947SAndroid Build Coastguard Worker */
chppGnssServiceRequestStateResyncCallback(void)496*84e33947SAndroid Build Coastguard Worker static void chppGnssServiceRequestStateResyncCallback(void) {
497*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *notification =
498*84e33947SAndroid Build Coastguard Worker chppAllocServiceNotificationFixed(struct ChppAppHeader);
499*84e33947SAndroid Build Coastguard Worker size_t notificationLen = sizeof(*notification);
500*84e33947SAndroid Build Coastguard Worker
501*84e33947SAndroid Build Coastguard Worker if (notification == NULL) {
502*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
503*84e33947SAndroid Build Coastguard Worker CHPP_ASSERT(false);
504*84e33947SAndroid Build Coastguard Worker
505*84e33947SAndroid Build Coastguard Worker } else {
506*84e33947SAndroid Build Coastguard Worker notification->handle = gGnssServiceContext.service.handle;
507*84e33947SAndroid Build Coastguard Worker notification->command = CHPP_GNSS_REQUEST_STATE_RESYNC_NOTIFICATION;
508*84e33947SAndroid Build Coastguard Worker
509*84e33947SAndroid Build Coastguard Worker chppEnqueueTxDatagramOrFail(
510*84e33947SAndroid Build Coastguard Worker gGnssServiceContext.service.appContext->transportContext, notification,
511*84e33947SAndroid Build Coastguard Worker notificationLen);
512*84e33947SAndroid Build Coastguard Worker }
513*84e33947SAndroid Build Coastguard Worker }
514*84e33947SAndroid Build Coastguard Worker
515*84e33947SAndroid Build Coastguard Worker /**
516*84e33947SAndroid Build Coastguard Worker * GNSS PAL callback to inform the CHRE of the result of changes to the location
517*84e33947SAndroid Build Coastguard Worker * session status.
518*84e33947SAndroid Build Coastguard Worker */
chppGnssServiceLocationStatusChangeCallback(bool enabled,uint8_t errorCode)519*84e33947SAndroid Build Coastguard Worker static void chppGnssServiceLocationStatusChangeCallback(bool enabled,
520*84e33947SAndroid Build Coastguard Worker uint8_t errorCode) {
521*84e33947SAndroid Build Coastguard Worker // Recreate request header
522*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader requestHeader = {
523*84e33947SAndroid Build Coastguard Worker .handle = gGnssServiceContext.service.handle,
524*84e33947SAndroid Build Coastguard Worker .transaction = gGnssServiceContext.controlLocationSession.transaction,
525*84e33947SAndroid Build Coastguard Worker .command = CHPP_GNSS_CONTROL_LOCATION_SESSION,
526*84e33947SAndroid Build Coastguard Worker };
527*84e33947SAndroid Build Coastguard Worker
528*84e33947SAndroid Build Coastguard Worker struct ChppGnssControlLocationSessionResponse *response =
529*84e33947SAndroid Build Coastguard Worker chppAllocResponseFixed(&requestHeader,
530*84e33947SAndroid Build Coastguard Worker struct ChppGnssControlLocationSessionResponse);
531*84e33947SAndroid Build Coastguard Worker size_t responseLen = sizeof(*response);
532*84e33947SAndroid Build Coastguard Worker
533*84e33947SAndroid Build Coastguard Worker if (response == NULL) {
534*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
535*84e33947SAndroid Build Coastguard Worker CHPP_ASSERT(false);
536*84e33947SAndroid Build Coastguard Worker
537*84e33947SAndroid Build Coastguard Worker } else {
538*84e33947SAndroid Build Coastguard Worker response->enabled = enabled;
539*84e33947SAndroid Build Coastguard Worker response->errorCode = errorCode;
540*84e33947SAndroid Build Coastguard Worker
541*84e33947SAndroid Build Coastguard Worker chppSendTimestampedResponseOrFail(
542*84e33947SAndroid Build Coastguard Worker gGnssServiceContext.service.appContext,
543*84e33947SAndroid Build Coastguard Worker &gGnssServiceContext.controlLocationSession, response, responseLen);
544*84e33947SAndroid Build Coastguard Worker }
545*84e33947SAndroid Build Coastguard Worker }
546*84e33947SAndroid Build Coastguard Worker
547*84e33947SAndroid Build Coastguard Worker /**
548*84e33947SAndroid Build Coastguard Worker * GNSS PAL callback to pass GNSS location fixes to the core CHRE system.
549*84e33947SAndroid Build Coastguard Worker */
chppGnssServiceLocationEventCallback(struct chreGnssLocationEvent * event)550*84e33947SAndroid Build Coastguard Worker static void chppGnssServiceLocationEventCallback(
551*84e33947SAndroid Build Coastguard Worker struct chreGnssLocationEvent *event) {
552*84e33947SAndroid Build Coastguard Worker // Craft response per parser script
553*84e33947SAndroid Build Coastguard Worker struct ChppGnssLocationEventWithHeader *notification = NULL;
554*84e33947SAndroid Build Coastguard Worker size_t notificationLen = 0;
555*84e33947SAndroid Build Coastguard Worker
556*84e33947SAndroid Build Coastguard Worker if (!chppGnssLocationEventFromChre(event, ¬ification, ¬ificationLen)) {
557*84e33947SAndroid Build Coastguard Worker CHPP_LOGE("LocationEvent conversion failed (OOM?)");
558*84e33947SAndroid Build Coastguard Worker
559*84e33947SAndroid Build Coastguard Worker notification = chppMalloc(sizeof(struct ChppAppHeader));
560*84e33947SAndroid Build Coastguard Worker if (notification == NULL) {
561*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
562*84e33947SAndroid Build Coastguard Worker } else {
563*84e33947SAndroid Build Coastguard Worker notificationLen = sizeof(struct ChppAppHeader);
564*84e33947SAndroid Build Coastguard Worker }
565*84e33947SAndroid Build Coastguard Worker }
566*84e33947SAndroid Build Coastguard Worker
567*84e33947SAndroid Build Coastguard Worker if (notification != NULL) {
568*84e33947SAndroid Build Coastguard Worker notification->header.handle = gGnssServiceContext.service.handle;
569*84e33947SAndroid Build Coastguard Worker notification->header.type = CHPP_MESSAGE_TYPE_SERVICE_NOTIFICATION;
570*84e33947SAndroid Build Coastguard Worker notification->header.transaction =
571*84e33947SAndroid Build Coastguard Worker 0; // Because we don't know this is in response to a Location Session
572*84e33947SAndroid Build Coastguard Worker // or Passive Location Listener
573*84e33947SAndroid Build Coastguard Worker notification->header.error =
574*84e33947SAndroid Build Coastguard Worker (notificationLen > sizeof(struct ChppAppHeader))
575*84e33947SAndroid Build Coastguard Worker ? CHPP_APP_ERROR_NONE
576*84e33947SAndroid Build Coastguard Worker : CHPP_APP_ERROR_CONVERSION_FAILED;
577*84e33947SAndroid Build Coastguard Worker notification->header.command = CHPP_GNSS_LOCATION_RESULT_NOTIFICATION;
578*84e33947SAndroid Build Coastguard Worker
579*84e33947SAndroid Build Coastguard Worker chppEnqueueTxDatagramOrFail(
580*84e33947SAndroid Build Coastguard Worker gGnssServiceContext.service.appContext->transportContext, notification,
581*84e33947SAndroid Build Coastguard Worker notificationLen);
582*84e33947SAndroid Build Coastguard Worker }
583*84e33947SAndroid Build Coastguard Worker
584*84e33947SAndroid Build Coastguard Worker gGnssServiceContext.api->releaseLocationEvent(event);
585*84e33947SAndroid Build Coastguard Worker }
586*84e33947SAndroid Build Coastguard Worker
587*84e33947SAndroid Build Coastguard Worker /**
588*84e33947SAndroid Build Coastguard Worker * GNSS PAL callback to inform the CHRE of the result of changes to the raw GNSS
589*84e33947SAndroid Build Coastguard Worker * measurement session status.
590*84e33947SAndroid Build Coastguard Worker */
chppGnssServiceMeasurementStatusChangeCallback(bool enabled,uint8_t errorCode)591*84e33947SAndroid Build Coastguard Worker static void chppGnssServiceMeasurementStatusChangeCallback(bool enabled,
592*84e33947SAndroid Build Coastguard Worker uint8_t errorCode) {
593*84e33947SAndroid Build Coastguard Worker // Recreate request header
594*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader requestHeader = {
595*84e33947SAndroid Build Coastguard Worker .handle = gGnssServiceContext.service.handle,
596*84e33947SAndroid Build Coastguard Worker .transaction = gGnssServiceContext.controlMeasurementSession.transaction,
597*84e33947SAndroid Build Coastguard Worker .command = CHPP_GNSS_CONTROL_MEASUREMENT_SESSION,
598*84e33947SAndroid Build Coastguard Worker };
599*84e33947SAndroid Build Coastguard Worker
600*84e33947SAndroid Build Coastguard Worker struct ChppGnssControlMeasurementSessionResponse *response =
601*84e33947SAndroid Build Coastguard Worker chppAllocResponseFixed(&requestHeader,
602*84e33947SAndroid Build Coastguard Worker struct ChppGnssControlMeasurementSessionResponse);
603*84e33947SAndroid Build Coastguard Worker size_t responseLen = sizeof(*response);
604*84e33947SAndroid Build Coastguard Worker
605*84e33947SAndroid Build Coastguard Worker if (response == NULL) {
606*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
607*84e33947SAndroid Build Coastguard Worker CHPP_ASSERT(false);
608*84e33947SAndroid Build Coastguard Worker
609*84e33947SAndroid Build Coastguard Worker } else {
610*84e33947SAndroid Build Coastguard Worker response->enabled = enabled;
611*84e33947SAndroid Build Coastguard Worker response->errorCode = errorCode;
612*84e33947SAndroid Build Coastguard Worker
613*84e33947SAndroid Build Coastguard Worker chppSendTimestampedResponseOrFail(
614*84e33947SAndroid Build Coastguard Worker gGnssServiceContext.service.appContext,
615*84e33947SAndroid Build Coastguard Worker &gGnssServiceContext.controlMeasurementSession, response, responseLen);
616*84e33947SAndroid Build Coastguard Worker }
617*84e33947SAndroid Build Coastguard Worker }
618*84e33947SAndroid Build Coastguard Worker
619*84e33947SAndroid Build Coastguard Worker /**
620*84e33947SAndroid Build Coastguard Worker * GNSS PAL callback to pass raw GNSS measurement data to the core CHRE system.
621*84e33947SAndroid Build Coastguard Worker */
chppGnssServiceMeasurementEventCallback(struct chreGnssDataEvent * event)622*84e33947SAndroid Build Coastguard Worker static void chppGnssServiceMeasurementEventCallback(
623*84e33947SAndroid Build Coastguard Worker struct chreGnssDataEvent *event) {
624*84e33947SAndroid Build Coastguard Worker // Craft response per parser script
625*84e33947SAndroid Build Coastguard Worker struct ChppGnssDataEventWithHeader *notification = NULL;
626*84e33947SAndroid Build Coastguard Worker size_t notificationLen = 0;
627*84e33947SAndroid Build Coastguard Worker
628*84e33947SAndroid Build Coastguard Worker if (!chppGnssDataEventFromChre(event, ¬ification, ¬ificationLen)) {
629*84e33947SAndroid Build Coastguard Worker CHPP_LOGE("DataEvent conversion failed (OOM?) ID=%" PRIu8,
630*84e33947SAndroid Build Coastguard Worker gGnssServiceContext.controlMeasurementSession.transaction);
631*84e33947SAndroid Build Coastguard Worker
632*84e33947SAndroid Build Coastguard Worker notification = chppMalloc(sizeof(struct ChppAppHeader));
633*84e33947SAndroid Build Coastguard Worker if (notification == NULL) {
634*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
635*84e33947SAndroid Build Coastguard Worker } else {
636*84e33947SAndroid Build Coastguard Worker notificationLen = sizeof(struct ChppAppHeader);
637*84e33947SAndroid Build Coastguard Worker }
638*84e33947SAndroid Build Coastguard Worker }
639*84e33947SAndroid Build Coastguard Worker
640*84e33947SAndroid Build Coastguard Worker if (notification != NULL) {
641*84e33947SAndroid Build Coastguard Worker notification->header.handle = gGnssServiceContext.service.handle;
642*84e33947SAndroid Build Coastguard Worker notification->header.type = CHPP_MESSAGE_TYPE_SERVICE_NOTIFICATION;
643*84e33947SAndroid Build Coastguard Worker notification->header.transaction =
644*84e33947SAndroid Build Coastguard Worker gGnssServiceContext.controlMeasurementSession.transaction;
645*84e33947SAndroid Build Coastguard Worker notification->header.error =
646*84e33947SAndroid Build Coastguard Worker (notificationLen > sizeof(struct ChppAppHeader))
647*84e33947SAndroid Build Coastguard Worker ? CHPP_APP_ERROR_NONE
648*84e33947SAndroid Build Coastguard Worker : CHPP_APP_ERROR_CONVERSION_FAILED;
649*84e33947SAndroid Build Coastguard Worker notification->header.command = CHPP_GNSS_MEASUREMENT_RESULT_NOTIFICATION;
650*84e33947SAndroid Build Coastguard Worker
651*84e33947SAndroid Build Coastguard Worker chppEnqueueTxDatagramOrFail(
652*84e33947SAndroid Build Coastguard Worker gGnssServiceContext.service.appContext->transportContext, notification,
653*84e33947SAndroid Build Coastguard Worker notificationLen);
654*84e33947SAndroid Build Coastguard Worker }
655*84e33947SAndroid Build Coastguard Worker
656*84e33947SAndroid Build Coastguard Worker gGnssServiceContext.api->releaseMeasurementDataEvent(event);
657*84e33947SAndroid Build Coastguard Worker }
658*84e33947SAndroid Build Coastguard Worker
659*84e33947SAndroid Build Coastguard Worker /************************************************
660*84e33947SAndroid Build Coastguard Worker * Public Functions
661*84e33947SAndroid Build Coastguard Worker ***********************************************/
662*84e33947SAndroid Build Coastguard Worker
chppRegisterGnssService(struct ChppAppState * appContext)663*84e33947SAndroid Build Coastguard Worker void chppRegisterGnssService(struct ChppAppState *appContext) {
664*84e33947SAndroid Build Coastguard Worker gGnssServiceContext.api = chrePalGnssGetApi(CHPP_PAL_GNSS_API_VERSION);
665*84e33947SAndroid Build Coastguard Worker
666*84e33947SAndroid Build Coastguard Worker if (gGnssServiceContext.api == NULL) {
667*84e33947SAndroid Build Coastguard Worker CHPP_DEBUG_ASSERT_LOG(false,
668*84e33947SAndroid Build Coastguard Worker "GNSS PAL API incompatible. Cannot register service");
669*84e33947SAndroid Build Coastguard Worker
670*84e33947SAndroid Build Coastguard Worker } else {
671*84e33947SAndroid Build Coastguard Worker chppRegisterService(appContext, (void *)&gGnssServiceContext,
672*84e33947SAndroid Build Coastguard Worker &gGnssServiceContext.service, NULL /*outReqState*/,
673*84e33947SAndroid Build Coastguard Worker &kGnssServiceConfig);
674*84e33947SAndroid Build Coastguard Worker CHPP_DEBUG_ASSERT(gGnssServiceContext.service.handle);
675*84e33947SAndroid Build Coastguard Worker }
676*84e33947SAndroid Build Coastguard Worker }
677*84e33947SAndroid Build Coastguard Worker
chppDeregisterGnssService(struct ChppAppState * appContext)678*84e33947SAndroid Build Coastguard Worker void chppDeregisterGnssService(struct ChppAppState *appContext) {
679*84e33947SAndroid Build Coastguard Worker // TODO
680*84e33947SAndroid Build Coastguard Worker
681*84e33947SAndroid Build Coastguard Worker UNUSED_VAR(appContext);
682*84e33947SAndroid Build Coastguard Worker }
683