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
25namespace chre {
26
27PlatformGnss::~PlatformGnss() {
28  if (mGnssApi != nullptr) {
29    LOGD("Platform GNSS closing");
30    prePalApiCall();
31    mGnssApi->close();
32    LOGD("Platform GNSS closed");
33  }
34}
35
36void PlatformGnss::init() {
37  prePalApiCall();
38  mGnssApi = chrePalGnssGetApi(CHRE_PAL_GNSS_API_CURRENT_VERSION);
39  if (mGnssApi != nullptr) {
40    mGnssCallbacks.requestStateResync =
41        PlatformGnssBase::requestStateResyncCallback;
42    mGnssCallbacks.locationStatusChangeCallback =
43        PlatformGnssBase::locationStatusChangeCallback;
44    mGnssCallbacks.locationEventCallback =
45        PlatformGnssBase::locationEventCallback;
46    mGnssCallbacks.measurementStatusChangeCallback =
47        PlatformGnssBase::measurementStatusChangeCallback;
48    mGnssCallbacks.measurementEventCallback =
49        PlatformGnssBase::measurementEventCallback;
50    if (!mGnssApi->open(&gChrePalSystemApi, &mGnssCallbacks)) {
51      LOGE("GNSS PAL open returned false");
52      mGnssApi = nullptr;
53    }
54  } else {
55    LOGW("Requested GNSS PAL (version %08" PRIx32 ") not found",
56         CHRE_PAL_GNSS_API_CURRENT_VERSION);
57  }
58}
59
60uint32_t PlatformGnss::getCapabilities() {
61  if (mGnssApi != nullptr) {
62    prePalApiCall();
63    return mGnssApi->getCapabilities();
64  } else {
65    return CHRE_GNSS_CAPABILITIES_NONE;
66  }
67}
68
69bool 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
81void PlatformGnss::releaseLocationEvent(chreGnssLocationEvent *event) {
82  if (mGnssApi != nullptr) {
83    prePalApiCall();
84    mGnssApi->releaseLocationEvent(event);
85  }
86}
87
88void PlatformGnssBase::requestStateResyncCallback() {
89  // TODO: Implement this.
90}
91
92void PlatformGnssBase::locationStatusChangeCallback(bool enabled,
93                                                    uint8_t errorCode) {
94  EventLoopManagerSingleton::get()->getGnssRequestManager()
95      .handleLocationSessionStatusChange(enabled, errorCode);
96}
97
98void PlatformGnssBase::locationEventCallback(
99    struct chreGnssLocationEvent *event) {
100  EventLoopManagerSingleton::get()->getGnssRequestManager()
101      .handleLocationEvent(event);
102}
103
104void PlatformGnssBase::measurementStatusChangeCallback(bool enabled,
105                                                       uint8_t errorCode) {
106  // TODO: Implement this.
107}
108
109void PlatformGnssBase::measurementEventCallback(
110    struct chreGnssDataEvent *event) {
111  // TODO: Implement this.
112}
113
114}  // namespace chre
115