aboutsummaryrefslogtreecommitdiff
path: root/chpp/clients/gnss.c
blob: b98a2db96cf03156b750ac869d3a7e7fd25214ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "chpp/clients/gnss.h"

#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>

#include "chpp/app.h"
#include "chpp/clients.h"
#include "chpp/clients/discovery.h"
#include "chpp/common/gnss.h"
#include "chpp/common/gnss_types.h"
#include "chpp/common/standard_uuids.h"
#include "chpp/log.h"
#include "chpp/macros.h"
#include "chpp/memory.h"
#include "chre/pal/gnss.h"
#include "chre_api/chre/gnss.h"

#ifndef CHPP_GNSS_DISCOVERY_TIMEOUT_MS
#define CHPP_GNSS_DISCOVERY_TIMEOUT_MS CHPP_DISCOVERY_DEFAULT_TIMEOUT_MS
#endif

/************************************************
 *  Prototypes
 ***********************************************/

static enum ChppAppErrorCode chppDispatchGnssResponse(void *clientContext,
                                                      uint8_t *buf, size_t len);
static enum ChppAppErrorCode chppDispatchGnssNotification(void *clientContext,
                                                          uint8_t *buf,
                                                          size_t len);
static bool chppGnssClientInit(void *clientContext, uint8_t handle,
                               struct ChppVersion serviceVersion);
static void chppGnssClientDeinit(void *clientContext);
static void chppGnssClientNotifyReset(void *clientContext);
static void chppGnssClientNotifyMatch(void *clientContext);

/************************************************
 *  Private Definitions
 ***********************************************/

/**
 * Structure to maintain state for the GNSS client and its Request/Response
 * (RR) functionality.
 */
struct ChppGnssClientState {
  struct ChppClientState client;     // GNSS client state
  const struct chrePalGnssApi *api;  // GNSS PAL API

  struct ChppRequestResponseState rRState[CHPP_GNSS_CLIENT_REQUEST_MAX + 1];

  uint32_t capabilities;           // Cached GetCapabilities result
  bool requestStateResyncPending;  // requestStateResync() is waiting to be
                                   // processed
};

// Note: This global definition of gGnssClientContext supports only one
// instance of the CHPP GNSS client at a time.
struct ChppGnssClientState gGnssClientContext;
static const struct chrePalSystemApi *gSystemApi;
static const struct chrePalGnssCallbacks *gCallbacks;

/**
 * Configuration parameters for this client
 */
static const struct ChppClient kGnssClientConfig = {
    .descriptor.uuid = CHPP_UUID_GNSS_STANDARD,

    // Version
    .descriptor.version.major = 1,
    .descriptor.version.minor = 0,
    .descriptor.version.patch = 0,

    // Notifies client if CHPP is reset
    .resetNotifierFunctionPtr = &chppGnssClientNotifyReset,

    // Notifies client if they are matched to a service
    .matchNotifierFunctionPtr = &chppGnssClientNotifyMatch,

    // Service response dispatch function pointer
    .responseDispatchFunctionPtr = &chppDispatchGnssResponse,

    // Service notification dispatch function pointer
    .notificationDispatchFunctionPtr = &chppDispatchGnssNotification,

    // Service response dispatch function pointer
    .initFunctionPtr = &chppGnssClientInit,

    // Service notification dispatch function pointer
    .deinitFunctionPtr = &chppGnssClientDeinit,

    // Number of request-response states in the rRStates array.
    .rRStateCount = ARRAY_SIZE(gGnssClientContext.rRState),

    // Min length is the entire header
    .minLength = sizeof(struct ChppAppHeader),
};

/************************************************
 *  Prototypes
 ***********************************************/

static bool chppGnssClientOpen(const struct chrePalSystemApi *systemApi,
                               const struct chrePalGnssCallbacks *callbacks);
static void chppGnssClientClose(void);
static uint32_t chppGnssClientGetCapabilities(void);
static bool chppGnssClientControlLocationSession(bool enable,
                                                 uint32_t minIntervalMs,
                                                 uint32_t minTimeToNextFixMs);
static void chppGnssClientReleaseLocationEvent(
    struct chreGnssLocationEvent *event);
static bool chppGnssClientControlMeasurementSession(bool enable,
                                                    uint32_t minIntervalMs);
static void chppGnssClientReleaseMeasurementDataEvent(
    struct chreGnssDataEvent *event);
static bool chppGnssClientConfigurePassiveLocationListener(bool enable);

static void chppGnssCloseResult(struct ChppGnssClientState *clientContext,
                                uint8_t *buf, size_t len);
static void chppGnssGetCapabilitiesResult(
    struct ChppGnssClientState *clientContext, uint8_t *buf, size_t len);
static void chppGnssControlLocationSessionResult(
    struct ChppGnssClientState *clientContext, uint8_t *buf, size_t len);
static void chppGnssControlMeasurementSessionResult(
    struct ChppGnssClientState *clientContext, uint8_t *buf, size_t len);
static void chppGnssConfigurePassiveLocationListenerResult(
    struct ChppGnssClientState *clientContext, uint8_t *buf, size_t len);

static void chppGnssStateResyncNotification(
    struct ChppGnssClientState *clientContext, uint8_t *buf, size_t len);
static void chppGnssLocationResultNotification(
    struct ChppGnssClientState *clientContext, uint8_t *buf, size_t len);
static void chppGnssMeasurementResultNotification(
    struct ChppGnssClientState *clientContext, uint8_t *buf, size_t len);

/************************************************
 *  Private Functions
 ***********************************************/

/**
 * Dispatches a service response from the transport layer that is determined to
 * be for the GNSS client.
 *
 * This function is called from the app layer using its function pointer given
 * during client registration.
 *
 * @param clientContext Maintains status for each client instance.
 * @param buf Input data. Cannot be null.
 * @param len Length of input data in bytes.
 *
 * @return Indicates the result of this function call.
 */
static enum ChppAppErrorCode chppDispatchGnssResponse(void *clientContext,
                                                      uint8_t *buf,
                                                      size_t len) {
  struct ChppAppHeader *rxHeader = (struct ChppAppHeader *)buf;
  struct ChppGnssClientState *gnssClientContext =
      (struct ChppGnssClientState *)clientContext;
  enum ChppAppErrorCode error = CHPP_APP_ERROR_NONE;

  if (rxHeader->command > CHPP_GNSS_CLIENT_REQUEST_MAX) {
    error = CHPP_APP_ERROR_INVALID_COMMAND;

  } else if (!chppClientTimestampResponse(
                 &gnssClientContext->client,
                 &gnssClientContext->rRState[rxHeader->command], rxHeader)) {
    error = CHPP_APP_ERROR_UNEXPECTED_RESPONSE;

  } else {
    switch (rxHeader->command) {
      case CHPP_GNSS_OPEN: {
        chppClientProcessOpenResponse(&gnssClientContext->client, buf, len);
        if (gnssClientContext->requestStateResyncPending) {
          gCallbacks->requestStateResync();
          gnssClientContext->requestStateResyncPending = false;
        }
        break;
      }

      case CHPP_GNSS_CLOSE: {
        chppGnssCloseResult(gnssClientContext, buf, len);
        break;
      }

      case CHPP_GNSS_GET_CAPABILITIES: {
        chppGnssGetCapabilitiesResult(gnssClientContext, buf, len);
        break;
      }

      case CHPP_GNSS_CONTROL_LOCATION_SESSION: {
        chppGnssControlLocationSessionResult(gnssClientContext, buf, len);
        break;
      }

      case CHPP_GNSS_CONTROL_MEASUREMENT_SESSION: {
        chppGnssControlMeasurementSessionResult(gnssClientContext, buf, len);
        break;
      }

      case CHPP_GNSS_CONFIGURE_PASSIVE_LOCATION_LISTENER: {
        chppGnssConfigurePassiveLocationListenerResult(gnssClientContext, buf,
                                                       len);
        break;
      }

      default: {
        error = CHPP_APP_ERROR_INVALID_COMMAND;
        break;
      }
    }
  }

  return error;
}

/**
 * Dispatches a service notification from the transport layer that is determined
 * to be for the GNSS client.
 *
 * This function is called from the app layer using its function pointer given
 * during client registration.
 *
 * @param clientContext Maintains status for each client instance.
 * @param buf Input data. Cannot be null.
 * @param len Length of input data in bytes.
 *
 * @return Indicates the result of this function call.
 */
static enum ChppAppErrorCode chppDispatchGnssNotification(void *clientContext,
                                                          uint8_t *buf,
                                                          size_t len) {
  struct ChppAppHeader *rxHeader = (struct ChppAppHeader *)buf;
  struct ChppGnssClientState *gnssClientContext =
      (struct ChppGnssClientState *)clientContext;
  enum ChppAppErrorCode error = CHPP_APP_ERROR_NONE;

  switch (rxHeader->command) {
    case CHPP_GNSS_REQUEST_STATE_RESYNC_NOTIFICATION: {
      chppGnssStateResyncNotification(gnssClientContext, buf, len);
      break;
    }

    case CHPP_GNSS_LOCATION_RESULT_NOTIFICATION: {
      chppGnssLocationResultNotification(gnssClientContext, buf, len);
      break;
    }

    case CHPP_GNSS_MEASUREMENT_RESULT_NOTIFICATION: {
      chppGnssMeasurementResultNotification(gnssClientContext, buf, len);
      break;
    }

    default: {
      error = CHPP_APP_ERROR_INVALID_COMMAND;
      break;
    }
  }

  return error;
}

/**
 * Initializes the client and provides its handle number and the version of the
 * matched service when/if it the client is matched with a service during
 * discovery.
 *
 * @param clientContext Maintains status for each client instance.
 * @param handle Handle number for this client.
 * @param serviceVersion Version of the matched service.
 *
 * @return True if client is compatible and successfully initialized.
 */
static bool chppGnssClientInit(void *clientContext, uint8_t handle,
                               struct ChppVersion serviceVersion) {
  UNUSED_VAR(serviceVersion);

  struct ChppGnssClientState *gnssClientContext =
      (struct ChppGnssClientState *)clientContext;
  chppClientInit(&gnssClientContext->client, handle);

  return true;
}

/**
 * Deinitializes the client.
 *
 * @param clientContext Maintains status for each client instance.
 */
static void chppGnssClientDeinit(void *clientContext) {
  struct ChppGnssClientState *gnssClientContext =
      (struct ChppGnssClientState *)clientContext;
  chppClientDeinit(&gnssClientContext->client);
}

/**
 * Notifies the client of an incoming reset.
 *
 * @param clientContext Maintains status for each client instance.
 */
static void chppGnssClientNotifyReset(void *clientContext) {
  struct ChppGnssClientState *gnssClientContext =
      (struct ChppGnssClientState *)clientContext;

  chppClientCloseOpenRequests(&gnssClientContext->client, &kGnssClientConfig,
                              false /* clearOnly */);

  if (gnssClientContext->client.openState != CHPP_OPEN_STATE_OPENED &&
      !gnssClientContext->client.pseudoOpen) {
    CHPP_LOGW("GNSS client reset but wasn't open");
  } else {
    CHPP_LOGI("GNSS client reopening from state=%" PRIu8,
              gnssClientContext->client.openState);
    gnssClientContext->requestStateResyncPending = true;
    chppClientSendOpenRequest(&gGnssClientContext.client,
                              &gGnssClientContext.rRState[CHPP_GNSS_OPEN],
                              CHPP_GNSS_OPEN,
                              /*blocking=*/false);
  }
}

/**
 * Notifies the client of being matched to a service.
 *
 * @param clientContext Maintains status for each client instance.
 */
static void chppGnssClientNotifyMatch(void *clientContext) {
  struct ChppGnssClientState *gnssClientContext =
      (struct ChppGnssClientState *)clientContext;

  if (gnssClientContext->client.pseudoOpen) {
    CHPP_LOGD("Pseudo-open GNSS client opening");
    chppClientSendOpenRequest(&gGnssClientContext.client,
                              &gGnssClientContext.rRState[CHPP_GNSS_OPEN],
                              CHPP_GNSS_OPEN,
                              /*blocking=*/false);
  }
}

/**
 * Handles the service response for the close client request.
 *
 * This function is called from chppDispatchGnssResponse().
 *
 * @param clientContext Maintains status for each client instance.
 * @param buf Input data. Cannot be null.
 * @param len Length of input data in bytes.
 */
static void chppGnssCloseResult(struct ChppGnssClientState *clientContext,
                                uint8_t *buf, size_t len) {
  // TODO
  UNUSED_VAR(clientContext);
  UNUSED_VAR(buf);
  UNUSED_VAR(len);
}

/**
 * Handles the service response for the get capabilities client request.
 *
 * This function is called from chppDispatchGnssResponse().
 *
 * @param clientContext Maintains status for each client instance.
 * @param buf Input data. Cannot be null.
 * @param len Length of input data in bytes.
 */
static void chppGnssGetCapabilitiesResult(
    struct ChppGnssClientState *clientContext, uint8_t *buf, size_t len) {
  if (len < sizeof(struct ChppGnssGetCapabilitiesResponse)) {
    struct ChppAppHeader *rxHeader = (struct ChppAppHeader *)buf;
    CHPP_LOGE("GetCapabilities resp. too short. err=%" PRIu8, rxHeader->error);

  } else {
    struct ChppGnssGetCapabilitiesParameters *result =
        &((struct ChppGnssGetCapabilitiesResponse *)buf)->params;

    CHPP_LOGD("chppGnssGetCapabilitiesResult received capabilities=0x%" PRIx32,
              result->capabilities);

#ifdef CHPP_GNSS_DEFAULT_CAPABILITIES
    CHPP_ASSERT_LOG((result->capabilities == CHPP_GNSS_DEFAULT_CAPABILITIES),
                    "Unexpected capability 0x%" PRIx32 " != 0x%" PRIx32,
                    result->capabilities, CHPP_GNSS_DEFAULT_CAPABILITIES);
#endif

    clientContext->capabilities = result->capabilities;
  }
}

/**
 * Handles the service response for the Control Location Session client request.
 *
 * This function is called from chppDispatchGnssResponse().
 *
 * @param clientContext Maintains status for each client instance.
 * @param buf Input data. Cannot be null.
 * @param len Length of input data in bytes.
 */
static void chppGnssControlLocationSessionResult(
    struct ChppGnssClientState *clientContext, uint8_t *buf, size_t len) {
  UNUSED_VAR(clientContext);

  if (len < sizeof(struct ChppGnssControlLocationSessionResponse)) {
    // Short response length indicates an error

    struct ChppAppHeader *rxHeader = (struct ChppAppHeader *)buf;
    CHPP_LOGE("ControlLocation resp. too short. err=%" PRIu8, rxHeader->error);

    if (rxHeader->error == CHPP_APP_ERROR_NONE) {
      rxHeader->error = CHPP_APP_ERROR_INVALID_LENGTH;
    }
    gCallbacks->locationStatusChangeCallback(
        false, chppAppErrorToChreError(rxHeader->error));

  } else {
    struct ChppGnssControlLocationSessionResponse *result =
        (struct ChppGnssControlLocationSessionResponse *)buf;

    CHPP_LOGD(
        "chppGnssControlLocationSessionResult received enable=%d, "
        "errorCode=%" PRIu8,
        result->enabled, result->errorCode);

    gCallbacks->locationStatusChangeCallback(result->enabled,
                                             result->errorCode);
  }
}

/**
 * Handles the service response for the Control Measurement Session client
 * request.
 *
 * This function is called from chppDispatchGnssResponse().
 *
 * @param clientContext Maintains status for each client instance.
 * @param buf Input data. Cannot be null.
 * @param len Length of input data in bytes.
 */
static void chppGnssControlMeasurementSessionResult(
    struct ChppGnssClientState *clientContext, uint8_t *buf, size_t len) {
  UNUSED_VAR(clientContext);

  if (len < sizeof(struct ChppGnssControlMeasurementSessionResponse)) {
    // Short response length indicates an error

    struct ChppAppHeader *rxHeader = (struct ChppAppHeader *)buf;
    CHPP_LOGE("Measurement resp. too short. err=%" PRIu8, rxHeader->error);

    if (rxHeader->error == CHPP_APP_ERROR_NONE) {
      rxHeader->error = CHPP_APP_ERROR_INVALID_LENGTH;
    }
    gCallbacks->measurementStatusChangeCallback(
        false, chppAppErrorToChreError(rxHeader->error));

  } else {
    struct ChppGnssControlMeasurementSessionResponse *result =
        (struct ChppGnssControlMeasurementSessionResponse *)buf;

    CHPP_LOGD(
        "chppGnssControlMeasurementSessionResult received enable=%d, "
        "errorCode=%" PRIu8,
        result->enabled, result->errorCode);

    gCallbacks->measurementStatusChangeCallback(result->enabled,
                                                result->errorCode);
  }
}

/**
 * Handles the service response for the Configure Passive Location Listener
 * client request.
 *
 * This function is called from chppDispatchGnssResponse().
 *
 * @param clientContext Maintains status for each client instance.
 * @param buf Input data. Cannot be null.
 * @param len Length of input data in bytes.
 */
static void chppGnssConfigurePassiveLocationListenerResult(
    struct ChppGnssClientState *clientContext, uint8_t *buf, size_t len) {
  UNUSED_VAR(clientContext);
  UNUSED_VAR(len);

  struct ChppAppHeader *rxHeader = (struct ChppAppHeader *)buf;

  if (rxHeader->error != CHPP_APP_ERROR_NONE) {
    CHPP_LOGE("Passive scan failed at service err=%" PRIu8, rxHeader->error);
    CHPP_DEBUG_ASSERT(false);

  } else {
    CHPP_LOGD(
        "WiFi ConfigurePassiveLocationListener request accepted at service");
  }
}

/**
 * Handles the State Resync service notification.
 *
 * This function is called from chppDispatchGnssNotification().
 *
 * @param clientContext Maintains status for each client instance.
 * @param buf Input data. Cannot be null.
 * @param len Length of input data in bytes.
 */
static void chppGnssStateResyncNotification(
    struct ChppGnssClientState *clientContext, uint8_t *buf, size_t len) {
  UNUSED_VAR(buf);
  UNUSED_VAR(len);
  if (clientContext->client.openState == CHPP_OPEN_STATE_WAITING_TO_OPEN) {
    // If the GNSS client is waiting for the open to proceed, the CHRE handler
    // for requestStateResync() may fail, so we set a flag to process it later
    // when the open has succeeded.
    clientContext->requestStateResyncPending = true;
  } else {
    gCallbacks->requestStateResync();
    clientContext->requestStateResyncPending = false;
  }
}

/**
 * Handles the Location Result service notification.
 *
 * This function is called from chppDispatchGnssNotification().
 *
 * @param clientContext Maintains status for each client instance.
 * @param buf Input data. Cannot be null.
 * @param len Length of input data in bytes.
 */
static void chppGnssLocationResultNotification(
    struct ChppGnssClientState *clientContext, uint8_t *buf, size_t len) {
  UNUSED_VAR(clientContext);
  CHPP_LOGD("chppGnssLocationResultNotification received data len=%" PRIuSIZE,
            len);

  buf += sizeof(struct ChppAppHeader);
  len -= sizeof(struct ChppAppHeader);

  struct chreGnssLocationEvent *chre =
      chppGnssLocationEventToChre((struct ChppGnssLocationEvent *)buf, len);

  if (chre == NULL) {
    CHPP_LOGE("Location result conversion failed: len=%" PRIuSIZE, len);
  } else {
    gCallbacks->locationEventCallback(chre);
  }
}

/**
 * Handles the Measurement Result service notification.
 *
 * This function is called from chppDispatchGnssNotification().
 *
 * @param clientContext Maintains status for each client instance.
 * @param buf Input data. Cannot be null.
 * @param len Length of input data in bytes.
 */
static void chppGnssMeasurementResultNotification(
    struct ChppGnssClientState *clientContext, uint8_t *buf, size_t len) {
  UNUSED_VAR(clientContext);
  CHPP_LOGD(
      "chppGnssMeasurementResultNotification received data len=%" PRIuSIZE,
      len);

  buf += sizeof(struct ChppAppHeader);
  len -= sizeof(struct ChppAppHeader);

  struct chreGnssDataEvent *chre =
      chppGnssDataEventToChre((struct ChppGnssDataEvent *)buf, len);

  if (chre == NULL) {
    CHPP_LOGE("Measurement result conversion failed len=%" PRIuSIZE, len);
  } else {
    gCallbacks->measurementEventCallback(chre);
  }
}

/**
 * Initializes the GNSS client upon an open request from CHRE and responds
 * with the result.
 *
 * @param systemApi CHRE system function pointers.
 * @param callbacks CHRE entry points.
 *
 * @return True if successful. False otherwise.
 */
static bool chppGnssClientOpen(const struct chrePalSystemApi *systemApi,
                               const struct chrePalGnssCallbacks *callbacks) {
  CHPP_DEBUG_ASSERT(systemApi != NULL);
  CHPP_DEBUG_ASSERT(callbacks != NULL);

  bool result = false;
  gSystemApi = systemApi;
  gCallbacks = callbacks;

  CHPP_LOGD("GNSS client opening");

  if (chppWaitForDiscoveryComplete(gGnssClientContext.client.appContext,
                                   CHPP_GNSS_DISCOVERY_TIMEOUT_MS)) {
    result = chppClientSendOpenRequest(
        &gGnssClientContext.client, &gGnssClientContext.rRState[CHPP_GNSS_OPEN],
        CHPP_GNSS_OPEN,
        /*blocking=*/true);
  }

#ifdef CHPP_GNSS_CLIENT_OPEN_ALWAYS_SUCCESS
  chppClientPseudoOpen(&gGnssClientContext.client);
  result = true;
#endif

  return result;
}

/**
 * Deinitializes the GNSS client.
 */
static void chppGnssClientClose(void) {
  // Remote
  struct ChppAppHeader *request = chppAllocClientRequestCommand(
      &gGnssClientContext.client, CHPP_GNSS_CLOSE);

  if (request == NULL) {
    CHPP_LOG_OOM();
  } else if (chppSendTimestampedRequestAndWait(
                 &gGnssClientContext.client,
                 &gGnssClientContext.rRState[CHPP_GNSS_CLOSE], request,
                 sizeof(*request))) {
    gGnssClientContext.client.openState = CHPP_OPEN_STATE_CLOSED;
    gGnssClientContext.capabilities = CHRE_GNSS_CAPABILITIES_NONE;
    chppClientCloseOpenRequests(&gGnssClientContext.client, &kGnssClientConfig,
                                true /* clearOnly */);
  }
}

/**
 * Retrieves a set of flags indicating the GNSS features supported by the
 * current implementation.
 *
 * @return Capabilities flags.
 */
static uint32_t chppGnssClientGetCapabilities(void) {
#ifdef CHPP_GNSS_DEFAULT_CAPABILITIES
  uint32_t capabilities = CHPP_GNSS_DEFAULT_CAPABILITIES;
#else
  uint32_t capabilities = CHRE_GNSS_CAPABILITIES_NONE;
#endif

  if (gGnssClientContext.capabilities != CHRE_GNSS_CAPABILITIES_NONE) {
    // Result already cached
    capabilities = gGnssClientContext.capabilities;

  } else {
    struct ChppAppHeader *request = chppAllocClientRequestCommand(
        &gGnssClientContext.client, CHPP_GNSS_GET_CAPABILITIES);

    if (request == NULL) {
      CHPP_LOG_OOM();
    } else {
      if (chppSendTimestampedRequestAndWait(
              &gGnssClientContext.client,
              &gGnssClientContext.rRState[CHPP_GNSS_GET_CAPABILITIES], request,
              sizeof(*request))) {
        // Success. gGnssClientContext.capabilities is now populated
        capabilities = gGnssClientContext.capabilities;
      }
    }
  }

  return capabilities;
}

/**
 * Start/stop/modify the GNSS location session used for clients of the CHRE
 * API.
 *
 * @param enable true to start/modify the session, false to stop the
 *        session. If false, other parameters are ignored.
 * @param minIntervalMs See chreGnssLocationSessionStartAsync()
 * @param minTimeToNextFixMs See chreGnssLocationSessionStartAsync()
 *
 * @return True indicates the request was sent off to the service.
 */

static bool chppGnssClientControlLocationSession(bool enable,
                                                 uint32_t minIntervalMs,
                                                 uint32_t minTimeToNextFixMs) {
  bool result = false;

  struct ChppGnssControlLocationSessionRequest *request =
      chppAllocClientRequestFixed(&gGnssClientContext.client,
                                  struct ChppGnssControlLocationSessionRequest);

  if (request == NULL) {
    CHPP_LOG_OOM();
  } else {
    request->header.command = CHPP_GNSS_CONTROL_LOCATION_SESSION;
    request->params.enable = enable;
    request->params.minIntervalMs = minIntervalMs;
    request->params.minTimeToNextFixMs = minTimeToNextFixMs;

    result = chppSendTimestampedRequestOrFail(
        &gGnssClientContext.client,
        &gGnssClientContext.rRState[CHPP_GNSS_CONTROL_LOCATION_SESSION],
        request, sizeof(*request), CHRE_GNSS_ASYNC_RESULT_TIMEOUT_NS);
  }

  return result;
}

/**
 * Releases the memory held for the location event callback.
 *
 * @param event Location event to be released.
 */
static void chppGnssClientReleaseLocationEvent(
    struct chreGnssLocationEvent *event) {
  CHPP_FREE_AND_NULLIFY(event);
}

/**
 * Start/stop/modify the raw GNSS measurement session used for clients of the
 * CHRE API.
 *
 * @param enable true to start/modify the session, false to stop the
 *        session. If false, other parameters are ignored.
 * @param minIntervalMs See chreGnssMeasurementSessionStartAsync()
 *
 * @return True indicates the request was sent off to the service.
 */

static bool chppGnssClientControlMeasurementSession(bool enable,
                                                    uint32_t minIntervalMs) {
  bool result = false;

  struct ChppGnssControlMeasurementSessionRequest *request =
      chppAllocClientRequestFixed(
          &gGnssClientContext.client,
          struct ChppGnssControlMeasurementSessionRequest);

  if (request == NULL) {
    CHPP_LOG_OOM();
  } else {
    request->header.command = CHPP_GNSS_CONTROL_MEASUREMENT_SESSION;
    request->params.enable = enable;
    request->params.minIntervalMs = minIntervalMs;

    result = chppSendTimestampedRequestOrFail(
        &gGnssClientContext.client,
        &gGnssClientContext.rRState[CHPP_GNSS_CONTROL_MEASUREMENT_SESSION],
        request, sizeof(*request), CHRE_GNSS_ASYNC_RESULT_TIMEOUT_NS);
  }

  return result;
}

/**
 * Releases the memory held for the measurement event callback.
 *
 * @param event Measurement event to be released.
 */
static void chppGnssClientReleaseMeasurementDataEvent(
    struct chreGnssDataEvent *event) {
  if (event->measurement_count > 0) {
    void *measurements = CHPP_CONST_CAST_POINTER(event->measurements);
    CHPP_FREE_AND_NULLIFY(measurements);
  }

  CHPP_FREE_AND_NULLIFY(event);
}

/**
 * Starts/stops opportunistic delivery of location fixes.
 *
 * @param enable true to turn the passive location listener on, false to
 *        turn it off.
 *
 * @return True indicates the request was sent off to the service.
 */
static bool chppGnssClientConfigurePassiveLocationListener(bool enable) {
  bool result = false;

  struct ChppGnssConfigurePassiveLocationListenerRequest *request =
      chppAllocClientRequestFixed(
          &gGnssClientContext.client,
          struct ChppGnssConfigurePassiveLocationListenerRequest);

  if (request == NULL) {
    CHPP_LOG_OOM();
  } else {
    request->header.command = CHPP_GNSS_CONFIGURE_PASSIVE_LOCATION_LISTENER;
    request->params.enable = enable;

    result = chppSendTimestampedRequestOrFail(
        &gGnssClientContext.client,
        &gGnssClientContext
             .rRState[CHPP_GNSS_CONFIGURE_PASSIVE_LOCATION_LISTENER],
        request, sizeof(*request), CHPP_CLIENT_REQUEST_TIMEOUT_DEFAULT);
  }

  return result;
}

/************************************************
 *  Public Functions
 ***********************************************/

void chppRegisterGnssClient(struct ChppAppState *appContext) {
  chppRegisterClient(appContext, (void *)&gGnssClientContext,
                     &gGnssClientContext.client, gGnssClientContext.rRState,
                     &kGnssClientConfig);
}

void chppDeregisterGnssClient(struct ChppAppState *appContext) {
  // TODO

  UNUSED_VAR(appContext);
}

struct ChppClientState *getChppGnssClientState(void) {
  return &gGnssClientContext.client;
}

#ifdef CHPP_CLIENT_ENABLED_GNSS

#ifdef CHPP_CLIENT_ENABLED_CHRE_GNSS
const struct chrePalGnssApi *chrePalGnssGetApi(uint32_t requestedApiVersion) {
#else
const struct chrePalGnssApi *chppPalGnssGetApi(uint32_t requestedApiVersion) {
#endif

  static const struct chrePalGnssApi api = {
      .moduleVersion = CHPP_PAL_GNSS_API_VERSION,
      .open = chppGnssClientOpen,
      .close = chppGnssClientClose,
      .getCapabilities = chppGnssClientGetCapabilities,
      .controlLocationSession = chppGnssClientControlLocationSession,
      .releaseLocationEvent = chppGnssClientReleaseLocationEvent,
      .controlMeasurementSession = chppGnssClientControlMeasurementSession,
      .releaseMeasurementDataEvent = chppGnssClientReleaseMeasurementDataEvent,
      .configurePassiveLocationListener =
          chppGnssClientConfigurePassiveLocationListener,
  };

  CHPP_STATIC_ASSERT(
      CHRE_PAL_GNSS_API_CURRENT_VERSION == CHPP_PAL_GNSS_API_VERSION,
      "A newer CHRE PAL API version is available. Please update.");

  if (!CHRE_PAL_VERSIONS_ARE_COMPATIBLE(api.moduleVersion,
                                        requestedApiVersion)) {
    return NULL;
  } else {
    return &api;
  }
}

#endif