gnss_world.cc revision e5d8b38a4814e88b04bcad8e8e9835c375e725bd
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 <chre.h>
18#include <cinttypes>
19
20#include "chre/util/nanoapp/log.h"
21#include "chre/util/time.h"
22
23#define LOG_TAG "[GnssWorld]"
24
25#ifdef CHRE_NANOAPP_INTERNAL
26namespace chre {
27namespace {
28#endif  // CHRE_NANOAPP_INTERNAL
29
30//! A dummy cookie to pass into the location session start async request.
31const uint32_t kLocationSessionStartCookie = 0x1337;
32
33//! The interval between location updates.
34constexpr Milliseconds kLocationInterval(Seconds(10));
35
36//! The minimum time to the next fix for a location.
37constexpr Milliseconds kLocationMinTimeToNextFix(0);
38
39void handleGnssAsyncResult(const chreAsyncResult *result) {
40  if (result->requestType == CHRE_GNSS_REQUEST_TYPE_LOCATION_SESSION_START) {
41    if (result->success) {
42      LOGI("Successfully requested a GNSS location session");
43    } else {
44      LOGE("Error requesting GNSS scan monitoring with %" PRIu8,
45           result->errorCode);
46    }
47
48    if (result->cookie != &kLocationSessionStartCookie) {
49      LOGE("Location session start request cookie mismatch");
50    }
51  } else {
52    LOGE("Received invalid async result");
53  }
54}
55
56void handleGnssLocationEvent(const chreGnssLocationEvent *event) {
57  LOGI("Received location: %" PRId32 ", %" PRId32, event->latitude_deg_e7,
58       event->longitude_deg_e7);
59  LOGI("  timestamp (ms): %" PRIu64, event->timestamp);
60  LOGI("  altitude (m): %f", event->altitude);
61  LOGI("  speed (m/s): %f", event->speed);
62  LOGI("  bearing (deg): %f", event->bearing);
63  LOGI("  accuracy: %f", event->accuracy);
64  LOGI("  flags: %" PRIx16, event->flags);
65}
66
67bool nanoappStart() {
68  LOGI("App started as instance %" PRIu32, chreGetInstanceId());
69
70  const char *gnssCapabilitiesStr;
71  uint32_t gnssCapabilities = chreGnssGetCapabilities();
72  switch (gnssCapabilities) {
73    case CHRE_GNSS_CAPABILITIES_LOCATION
74        | CHRE_GNSS_CAPABILITIES_MEASUREMENTS:
75      gnssCapabilitiesStr = "LOCATION | MEASUREMENTS";
76      break;
77    case CHRE_GNSS_CAPABILITIES_LOCATION:
78      gnssCapabilitiesStr = "LOCATION";
79      break;
80    case CHRE_GNSS_CAPABILITIES_MEASUREMENTS:
81      gnssCapabilitiesStr = "MEASUREMENTS";
82      break;
83    case CHRE_GNSS_CAPABILITIES_NONE:
84      gnssCapabilitiesStr = "NONE";
85      break;
86    default:
87      gnssCapabilitiesStr = "INVALID";
88  }
89
90  LOGI("Detected GNSS support as: %s (%" PRIu32 ")",
91       gnssCapabilitiesStr, gnssCapabilities);
92
93  if (gnssCapabilities & CHRE_GNSS_CAPABILITIES_LOCATION) {
94    if (chreGnssLocationSessionStartAsync(
95        kLocationInterval.getMilliseconds(),
96        kLocationMinTimeToNextFix.getMilliseconds(),
97        &kLocationSessionStartCookie)) {
98      LOGI("Location session start request sent");
99    } else {
100      LOGE("Error sending location session request");
101    }
102  }
103
104  return true;
105}
106
107void nanoappHandleEvent(uint32_t senderInstanceId,
108                        uint16_t eventType,
109                        const void *eventData) {
110  switch (eventType) {
111    case CHRE_EVENT_GNSS_ASYNC_RESULT:
112      handleGnssAsyncResult(static_cast<const chreAsyncResult *>(eventData));
113      break;
114    case CHRE_EVENT_GNSS_LOCATION:
115      handleGnssLocationEvent(
116          static_cast<const chreGnssLocationEvent *>(eventData));
117      break;
118    case CHRE_EVENT_NANOAPP_STARTED:
119    case CHRE_EVENT_NANOAPP_STOPPED:
120      // We don't use these events in this app
121      break;
122    default:
123      LOGW("Unhandled event type %" PRIu16, eventType);
124  }
125}
126
127void nanoappEnd() {
128  LOGI("Stopped");
129}
130
131#ifdef CHRE_NANOAPP_INTERNAL
132}  // anonymous namespace
133}  // namespace chre
134
135#include "chre/util/nanoapp/app_id.h"
136#include "chre/platform/static_nanoapp_init.h"
137
138CHRE_STATIC_NANOAPP_INIT(GnssWorld, chre::kGnssWorldAppId, 0);
139#endif  // CHRE_NANOAPP_INTERNAL
140