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/platform/platform_gnss.h"
18
19 #include <cinttypes>
20
21 #include "chre/core/event_loop_manager.h"
22 #include "chre/platform/log.h"
23 #include "chre/platform/shared/pal_system_api.h"
24
25 namespace chre {
26
27 const chrePalGnssCallbacks PlatformGnssBase::sGnssCallbacks = {
28 PlatformGnssBase::requestStateResyncCallback,
29 PlatformGnssBase::locationStatusChangeCallback,
30 PlatformGnssBase::locationEventCallback,
31 PlatformGnssBase::measurementStatusChangeCallback,
32 PlatformGnssBase::measurementEventCallback,
33 };
34
~PlatformGnss()35 PlatformGnss::~PlatformGnss() {
36 if (mGnssApi != nullptr) {
37 LOGD("Platform GNSS closing");
38 prePalApiCall();
39 mGnssApi->close();
40 LOGD("Platform GNSS closed");
41 }
42 }
43
init()44 void PlatformGnss::init() {
45 prePalApiCall();
46 mGnssApi = chrePalGnssGetApi(CHRE_PAL_GNSS_API_CURRENT_VERSION);
47 if (mGnssApi != nullptr) {
48 if (!mGnssApi->open(&gChrePalSystemApi, &sGnssCallbacks)) {
49 LOGE("GNSS PAL open returned false");
50 mGnssApi = nullptr;
51 } else {
52 LOGD("Opened GNSS PAL version 0x%08" PRIx32, mGnssApi->moduleVersion);
53 }
54 } else {
55 LOGW("Requested GNSS PAL (version 0x%08" PRIx32 ") not found",
56 CHRE_PAL_GNSS_API_CURRENT_VERSION);
57 }
58 }
59
getCapabilities()60 uint32_t PlatformGnss::getCapabilities() {
61 if (mGnssApi != nullptr) {
62 prePalApiCall();
63 return mGnssApi->getCapabilities();
64 } else {
65 return CHRE_GNSS_CAPABILITIES_NONE;
66 }
67 }
68
controlLocationSession(bool enable,Milliseconds minInterval,Milliseconds minTimeToNextFix)69 bool PlatformGnss::controlLocationSession(bool enable, Milliseconds minInterval,
70 Milliseconds minTimeToNextFix) {
71 if (mGnssApi != nullptr) {
72 prePalApiCall();
73 return mGnssApi->controlLocationSession(
74 enable, static_cast<uint32_t>(minInterval.getMilliseconds()),
75 static_cast<uint32_t>(minTimeToNextFix.getMilliseconds()));
76 } else {
77 return false;
78 }
79 }
80
releaseLocationEvent(chreGnssLocationEvent * event)81 void PlatformGnss::releaseLocationEvent(chreGnssLocationEvent *event) {
82 if (mGnssApi != nullptr) {
83 prePalApiCall();
84 mGnssApi->releaseLocationEvent(event);
85 }
86 }
87
requestStateResyncCallback()88 void PlatformGnssBase::requestStateResyncCallback() {
89 // TODO: Implement this.
90 }
91
locationStatusChangeCallback(bool enabled,uint8_t errorCode)92 void PlatformGnssBase::locationStatusChangeCallback(bool enabled,
93 uint8_t errorCode) {
94 EventLoopManagerSingleton::get()
95 ->getGnssManager()
96 .getLocationSession()
97 .handleStatusChange(enabled, errorCode);
98 }
99
locationEventCallback(struct chreGnssLocationEvent * event)100 void PlatformGnssBase::locationEventCallback(
101 struct chreGnssLocationEvent *event) {
102 EventLoopManagerSingleton::get()
103 ->getGnssManager()
104 .getLocationSession()
105 .handleReportEvent(event);
106 }
107
controlMeasurementSession(bool enable,Milliseconds minInterval)108 bool PlatformGnss::controlMeasurementSession(bool enable,
109 Milliseconds minInterval) {
110 if (mGnssApi != nullptr) {
111 prePalApiCall();
112 return mGnssApi->controlMeasurementSession(
113 enable, static_cast<uint32_t>(minInterval.getMilliseconds()));
114 } else {
115 return false;
116 }
117 }
118
releaseMeasurementDataEvent(chreGnssDataEvent * event)119 void PlatformGnss::releaseMeasurementDataEvent(chreGnssDataEvent *event) {
120 if (mGnssApi != nullptr) {
121 prePalApiCall();
122 mGnssApi->releaseMeasurementDataEvent(event);
123 }
124 }
125
measurementStatusChangeCallback(bool enabled,uint8_t errorCode)126 void PlatformGnssBase::measurementStatusChangeCallback(bool enabled,
127 uint8_t errorCode) {
128 EventLoopManagerSingleton::get()
129 ->getGnssManager()
130 .getMeasurementSession()
131 .handleStatusChange(enabled, errorCode);
132 }
133
measurementEventCallback(struct chreGnssDataEvent * event)134 void PlatformGnssBase::measurementEventCallback(
135 struct chreGnssDataEvent *event) {
136 EventLoopManagerSingleton::get()
137 ->getGnssManager()
138 .getMeasurementSession()
139 .handleReportEvent(event);
140 }
141
142 } // namespace chre
143