1/*
2 * Copyright (C) 2016 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#define LOG_TAG "GnssHAL_GnssXtraInterface"
18
19#include "GnssXtra.h"
20
21namespace android {
22namespace hardware {
23namespace gnss {
24namespace V1_0 {
25namespace implementation {
26
27std::vector<std::unique_ptr<ThreadFuncArgs>> GnssXtra::sThreadFuncArgsList;
28sp<IGnssXtraCallback> GnssXtra::sGnssXtraCbIface = nullptr;
29bool GnssXtra::sInterfaceExists = false;
30
31GpsXtraCallbacks GnssXtra::sGnssXtraCb = {
32    .download_request_cb = gnssXtraDownloadRequestCb,
33    .create_thread_cb = createThreadCb,
34};
35
36GnssXtra::~GnssXtra() {
37    sThreadFuncArgsList.clear();
38    sInterfaceExists = false;
39}
40
41pthread_t GnssXtra::createThreadCb(const char* name, void (*start)(void*), void* arg) {
42    return createPthread(name, start, arg, &sThreadFuncArgsList);
43}
44
45GnssXtra::GnssXtra(const GpsXtraInterface* xtraIface) : mGnssXtraIface(xtraIface) {
46    /* Error out if an instance of the interface already exists. */
47    LOG_ALWAYS_FATAL_IF(sInterfaceExists);
48    sInterfaceExists = true;
49}
50
51void GnssXtra::gnssXtraDownloadRequestCb() {
52    if (sGnssXtraCbIface == nullptr) {
53        ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
54        return;
55    }
56
57    auto ret = sGnssXtraCbIface->downloadRequestCb();
58    if (!ret.isOk()) {
59        ALOGE("%s: Unable to invoke callback", __func__);
60    }
61}
62
63// Methods from ::android::hardware::gnss::V1_0::IGnssXtra follow.
64Return<bool> GnssXtra::setCallback(const sp<IGnssXtraCallback>& callback)  {
65    if (mGnssXtraIface == nullptr) {
66        ALOGE("%s: Gnss Xtra interface is unavailable", __func__);
67        return false;
68    }
69
70    sGnssXtraCbIface = callback;
71
72    return (mGnssXtraIface->init(&sGnssXtraCb) == 0);
73}
74
75Return<bool> GnssXtra::injectXtraData(const hidl_string& xtraData)  {
76    if (mGnssXtraIface == nullptr) {
77        ALOGE("%s: Gnss Xtra interface is unavailable", __func__);
78        return false;
79    }
80
81    char* buf = new char[xtraData.size()];
82    const char* data = xtraData.c_str();
83
84    memcpy(buf, data, xtraData.size());
85
86    int ret = mGnssXtraIface->inject_xtra_data(buf, xtraData.size());
87    delete[] buf;
88    return (ret == 0);
89}
90
91}  // namespace implementation
92}  // namespace V1_0
93}  // namespace gnss
94}  // namespace hardware
95}  // namespace android
96