Hridya Valsaraju | 29dc1e0 | 2016-10-21 14:41:12 -0700 | [diff] [blame] | 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_GnssNiInterface" |
| 18 | |
| 19 | #include "GnssNi.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>> GnssNi::sThreadFuncArgsList; |
| 28 | sp<IGnssNiCallback> GnssNi::sGnssNiCbIface = nullptr; |
| 29 | bool GnssNi::sInterfaceExists = false; |
| 30 | |
| 31 | GpsNiCallbacks GnssNi::sGnssNiCb = { |
| 32 | .notify_cb = niNotifyCb, |
| 33 | .create_thread_cb = createThreadCb |
| 34 | }; |
| 35 | |
| 36 | GnssNi::GnssNi(const GpsNiInterface* gpsNiIface) : mGnssNiIface(gpsNiIface) { |
| 37 | /* Error out if an instance of the interface already exists. */ |
| 38 | LOG_ALWAYS_FATAL_IF(sInterfaceExists); |
| 39 | sInterfaceExists = true; |
| 40 | } |
| 41 | |
| 42 | GnssNi::~GnssNi() { |
| 43 | sThreadFuncArgsList.clear(); |
| 44 | } |
| 45 | |
| 46 | pthread_t GnssNi::createThreadCb(const char* name, void (*start)(void*), void* arg) { |
| 47 | return createPthread(name, start, arg, &sThreadFuncArgsList); |
| 48 | } |
| 49 | |
| 50 | void GnssNi::niNotifyCb(GpsNiNotification* notification) { |
| 51 | if (sGnssNiCbIface == nullptr) { |
| 52 | ALOGE("%s: GNSS NI Callback Interface configured incorrectly", __func__); |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | if (notification == nullptr) { |
| 57 | ALOGE("%s: Invalid GpsNotification callback from GNSS HAL", __func__); |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | IGnssNiCallback::GnssNiNotification notificationGnss = { |
| 62 | .notificationId = notification->notification_id, |
| 63 | .niType = static_cast<IGnssNiCallback::GnssNiType>(notification->ni_type), |
Yifan Hong | 7037fdb | 2016-12-05 17:16:09 -0800 | [diff] [blame] | 64 | .notifyFlags = notification->notify_flags, |
Hridya Valsaraju | 29dc1e0 | 2016-10-21 14:41:12 -0700 | [diff] [blame] | 65 | .timeoutSec = static_cast<uint32_t>(notification->timeout), |
| 66 | .defaultResponse = |
| 67 | static_cast<IGnssNiCallback::GnssUserResponseType>(notification->default_response), |
| 68 | .requestorId = notification->requestor_id, |
| 69 | .notificationMessage = notification->text, |
| 70 | .requestorIdEncoding = |
| 71 | static_cast<IGnssNiCallback::GnssNiEncodingType>(notification->requestor_id_encoding), |
| 72 | .notificationIdEncoding = |
| 73 | static_cast<IGnssNiCallback::GnssNiEncodingType>(notification->text_encoding) |
| 74 | }; |
| 75 | |
| 76 | sGnssNiCbIface->niNotifyCb(notificationGnss); |
| 77 | } |
| 78 | |
| 79 | // Methods from ::android::hardware::gnss::V1_0::IGnssNi follow. |
| 80 | Return<void> GnssNi::setCallback(const sp<IGnssNiCallback>& callback) { |
| 81 | if (mGnssNiIface == nullptr) { |
| 82 | ALOGE("%s: GnssNi interface is unavailable", __func__); |
| 83 | return Void(); |
| 84 | } |
| 85 | |
| 86 | sGnssNiCbIface = callback; |
| 87 | |
| 88 | mGnssNiIface->init(&sGnssNiCb); |
| 89 | return Void(); |
| 90 | } |
| 91 | |
| 92 | Return<void> GnssNi::respond(int32_t notifId, IGnssNiCallback::GnssUserResponseType userResponse) { |
| 93 | if (mGnssNiIface == nullptr) { |
| 94 | ALOGE("%s: GnssNi interface is unavailable", __func__); |
| 95 | } else { |
| 96 | mGnssNiIface->respond(notifId, static_cast<GpsUserResponseType>(userResponse)); |
| 97 | } |
| 98 | return Void(); |
| 99 | } |
| 100 | |
| 101 | } // namespace implementation |
| 102 | } // namespace V1_0 |
| 103 | } // namespace gnss |
| 104 | } // namespace hardware |
| 105 | } // namespace android |