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/shared/pal_system_api.h"
23 #include "chre/platform/log.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(enable,
74         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()->getGnssManager().getLocationSession()
95       .handleStatusChange(enabled, errorCode);
96 }
97 
locationEventCallback(struct chreGnssLocationEvent * event)98 void PlatformGnssBase::locationEventCallback(
99     struct chreGnssLocationEvent *event) {
100   EventLoopManagerSingleton::get()->getGnssManager().getLocationSession()
101       .handleReportEvent(event);
102 }
103 
controlMeasurementSession(bool enable,Milliseconds minInterval)104 bool PlatformGnss::controlMeasurementSession(bool enable,
105                                              Milliseconds minInterval) {
106   if (mGnssApi != nullptr) {
107     prePalApiCall();
108     return mGnssApi->controlMeasurementSession(enable,
109         static_cast<uint32_t>(minInterval.getMilliseconds()));
110   } else {
111     return false;
112   }
113 }
114 
releaseMeasurementDataEvent(chreGnssDataEvent * event)115 void PlatformGnss::releaseMeasurementDataEvent(chreGnssDataEvent *event) {
116   if (mGnssApi != nullptr) {
117     prePalApiCall();
118     mGnssApi->releaseMeasurementDataEvent(event);
119   }
120 }
121 
measurementStatusChangeCallback(bool enabled,uint8_t errorCode)122 void PlatformGnssBase::measurementStatusChangeCallback(bool enabled,
123                                                        uint8_t errorCode) {
124   EventLoopManagerSingleton::get()->getGnssManager().getMeasurementSession()
125       .handleStatusChange(enabled, errorCode);
126 }
127 
measurementEventCallback(struct chreGnssDataEvent * event)128 void PlatformGnssBase::measurementEventCallback(
129     struct chreGnssDataEvent *event) {
130   EventLoopManagerSingleton::get()->getGnssManager().getMeasurementSession()
131       .handleReportEvent(event);
132 }
133 
134 }  // namespace chre
135