blob: 10af1f4e879574f34324e96ed24c66628bff9159 [file] [log] [blame]
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -07001/*
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
21namespace android {
22namespace hardware {
23namespace gnss {
24namespace V1_0 {
25namespace implementation {
26
27std::vector<std::unique_ptr<ThreadFuncArgs>> GnssNi::sThreadFuncArgsList;
28sp<IGnssNiCallback> GnssNi::sGnssNiCbIface = nullptr;
29bool GnssNi::sInterfaceExists = false;
30
31GpsNiCallbacks GnssNi::sGnssNiCb = {
32 .notify_cb = niNotifyCb,
33 .create_thread_cb = createThreadCb
34};
35
36GnssNi::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
42GnssNi::~GnssNi() {
43 sThreadFuncArgsList.clear();
44}
45
46pthread_t GnssNi::createThreadCb(const char* name, void (*start)(void*), void* arg) {
47 return createPthread(name, start, arg, &sThreadFuncArgsList);
48}
49
50void 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),
64 .notifyFlags =
65 static_cast<IGnssNiCallback::GnssNiNotifyFlags>(notification->notify_flags),
66 .timeoutSec = static_cast<uint32_t>(notification->timeout),
67 .defaultResponse =
68 static_cast<IGnssNiCallback::GnssUserResponseType>(notification->default_response),
69 .requestorId = notification->requestor_id,
70 .notificationMessage = notification->text,
71 .requestorIdEncoding =
72 static_cast<IGnssNiCallback::GnssNiEncodingType>(notification->requestor_id_encoding),
73 .notificationIdEncoding =
74 static_cast<IGnssNiCallback::GnssNiEncodingType>(notification->text_encoding)
75 };
76
77 sGnssNiCbIface->niNotifyCb(notificationGnss);
78}
79
80// Methods from ::android::hardware::gnss::V1_0::IGnssNi follow.
81Return<void> GnssNi::setCallback(const sp<IGnssNiCallback>& callback) {
82 if (mGnssNiIface == nullptr) {
83 ALOGE("%s: GnssNi interface is unavailable", __func__);
84 return Void();
85 }
86
87 sGnssNiCbIface = callback;
88
89 mGnssNiIface->init(&sGnssNiCb);
90 return Void();
91}
92
93Return<void> GnssNi::respond(int32_t notifId, IGnssNiCallback::GnssUserResponseType userResponse) {
94 if (mGnssNiIface == nullptr) {
95 ALOGE("%s: GnssNi interface is unavailable", __func__);
96 } else {
97 mGnssNiIface->respond(notifId, static_cast<GpsUserResponseType>(userResponse));
98 }
99 return Void();
100}
101
102} // namespace implementation
103} // namespace V1_0
104} // namespace gnss
105} // namespace hardware
106} // namespace android