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 
21 namespace android {
22 namespace hardware {
23 namespace gnss {
24 namespace V1_0 {
25 namespace implementation {
26 
27 std::vector<std::unique_ptr<ThreadFuncArgs>> GnssXtra::sThreadFuncArgsList;
28 sp<IGnssXtraCallback> GnssXtra::sGnssXtraCbIface = nullptr;
29 bool GnssXtra::sInterfaceExists = false;
30 
31 GpsXtraCallbacks GnssXtra::sGnssXtraCb = {
32     .download_request_cb = gnssXtraDownloadRequestCb,
33     .create_thread_cb = createThreadCb,
34 };
35 
~GnssXtra()36 GnssXtra::~GnssXtra() {
37     sThreadFuncArgsList.clear();
38     sInterfaceExists = false;
39 }
40 
createThreadCb(const char * name,void (* start)(void *),void * arg)41 pthread_t GnssXtra::createThreadCb(const char* name, void (*start)(void*), void* arg) {
42     return createPthread(name, start, arg, &sThreadFuncArgsList);
43 }
44 
GnssXtra(const GpsXtraInterface * xtraIface)45 GnssXtra::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 
gnssXtraDownloadRequestCb()51 void 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.
setCallback(const sp<IGnssXtraCallback> & callback)64 Return<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 
injectXtraData(const hidl_string & xtraData)75 Return<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