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   EventLoopManagerSingleton::get()
90       ->getGnssManager()
91       .handleRequestStateResyncCallback();
92 }
93 
locationStatusChangeCallback(bool enabled,uint8_t errorCode)94 void PlatformGnssBase::locationStatusChangeCallback(bool enabled,
95                                                     uint8_t errorCode) {
96   EventLoopManagerSingleton::get()
97       ->getGnssManager()
98       .getLocationSession()
99       .handleStatusChange(enabled, errorCode);
100 }
101 
locationEventCallback(struct chreGnssLocationEvent * event)102 void PlatformGnssBase::locationEventCallback(
103     struct chreGnssLocationEvent *event) {
104   EventLoopManagerSingleton::get()
105       ->getGnssManager()
106       .getLocationSession()
107       .handleReportEvent(event);
108 }
109 
controlMeasurementSession(bool enable,Milliseconds minInterval)110 bool PlatformGnss::controlMeasurementSession(bool enable,
111                                              Milliseconds minInterval) {
112   if (mGnssApi != nullptr) {
113     prePalApiCall();
114     return mGnssApi->controlMeasurementSession(
115         enable, static_cast<uint32_t>(minInterval.getMilliseconds()));
116   } else {
117     return false;
118   }
119 }
120 
releaseMeasurementDataEvent(chreGnssDataEvent * event)121 void PlatformGnss::releaseMeasurementDataEvent(chreGnssDataEvent *event) {
122   if (mGnssApi != nullptr) {
123     prePalApiCall();
124     mGnssApi->releaseMeasurementDataEvent(event);
125   }
126 }
127 
configurePassiveLocationListener(bool enable)128 bool PlatformGnss::configurePassiveLocationListener(bool enable) {
129   bool success = false;
130   if (mGnssApi != nullptr &&
131       mGnssApi->moduleVersion >= CHRE_PAL_GNSS_API_V1_2) {
132     prePalApiCall();
133     success = mGnssApi->configurePassiveLocationListener(enable);
134   }
135   return success;
136 }
137 
measurementStatusChangeCallback(bool enabled,uint8_t errorCode)138 void PlatformGnssBase::measurementStatusChangeCallback(bool enabled,
139                                                        uint8_t errorCode) {
140   EventLoopManagerSingleton::get()
141       ->getGnssManager()
142       .getMeasurementSession()
143       .handleStatusChange(enabled, errorCode);
144 }
145 
measurementEventCallback(struct chreGnssDataEvent * event)146 void PlatformGnssBase::measurementEventCallback(
147     struct chreGnssDataEvent *event) {
148   EventLoopManagerSingleton::get()
149       ->getGnssManager()
150       .getMeasurementSession()
151       .handleReportEvent(event);
152 }
153 
154 }  // namespace chre
155