1 /*
2 * Copyright (C) 2021 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 #ifndef _ANDROID_SERVER_GNSS_GNSSNAVIGATIONMESSAGECALLBACK_H
18 #define _ANDROID_SERVER_GNSS_GNSSNAVIGATIONMESSAGECALLBACK_H
19
20 #pragma once
21
22 #ifndef LOG_TAG
23 #error LOG_TAG must be defined before including this file.
24 #endif
25
26 #include <android/hardware/gnss/1.0/IGnssNavigationMessage.h>
27 #include <android/hardware/gnss/BnGnssNavigationMessageCallback.h>
28 #include <log/log.h>
29
30 #include <vector>
31
32 #include "Utils.h"
33 #include "jni.h"
34
35 namespace android::gnss {
36
37 namespace {
38
39 extern jclass class_gnssNavigationMessage;
40 extern jmethodID method_reportNavigationMessages;
41 extern jmethodID method_gnssNavigationMessageCtor;
42
43 } // anonymous namespace
44
45 void GnssNavigationMessage_class_init_once(JNIEnv* env, jclass clazz);
46
47 class GnssNavigationMessageCallbackAidl : public hardware::gnss::BnGnssNavigationMessageCallback {
48 public:
GnssNavigationMessageCallbackAidl()49 GnssNavigationMessageCallbackAidl() {}
50 android::binder::Status gnssNavigationMessageCb(
51 const hardware::gnss::IGnssNavigationMessageCallback::GnssNavigationMessage& message)
52 override;
53 };
54
55 class GnssNavigationMessageCallbackHidl
56 : public hardware::gnss::V1_0::IGnssNavigationMessageCallback {
57 public:
GnssNavigationMessageCallbackHidl()58 GnssNavigationMessageCallbackHidl() {}
59
60 hardware::Return<void> gnssNavigationMessageCb(
61 const hardware::gnss::V1_0::IGnssNavigationMessageCallback::GnssNavigationMessage&
62 message) override;
63 };
64
65 class GnssNavigationMessageCallback {
66 public:
GnssNavigationMessageCallback()67 GnssNavigationMessageCallback() {}
getAidl()68 sp<GnssNavigationMessageCallbackAidl> getAidl() {
69 if (callbackAidl == nullptr) {
70 callbackAidl = sp<GnssNavigationMessageCallbackAidl>::make();
71 }
72 return callbackAidl;
73 }
74
getHidl()75 sp<GnssNavigationMessageCallbackHidl> getHidl() {
76 if (callbackHidl == nullptr) {
77 callbackHidl = sp<GnssNavigationMessageCallbackHidl>::make();
78 }
79 return callbackHidl;
80 }
81
82 private:
83 sp<GnssNavigationMessageCallbackAidl> callbackAidl;
84 sp<GnssNavigationMessageCallbackHidl> callbackHidl;
85 };
86
87 struct GnssNavigationMessageCallbackUtil {
88 template <class T>
89 static void gnssNavigationMessageCbImpl(const T& message);
90
91 private:
92 GnssNavigationMessageCallbackUtil() = delete;
93 };
94
95 template <class T>
gnssNavigationMessageCbImpl(const T & message)96 void GnssNavigationMessageCallbackUtil::gnssNavigationMessageCbImpl(const T& message) {
97 JNIEnv* env = getJniEnv();
98
99 size_t dataLength = message.data.size();
100
101 std::vector<uint8_t> navigationData = message.data;
102 uint8_t* data = &(navigationData[0]);
103 if (dataLength == 0 || data == nullptr) {
104 ALOGE("Invalid Navigation Message found: data=%p, length=%zd", data, dataLength);
105 return;
106 }
107
108 JavaObject object(env, class_gnssNavigationMessage, method_gnssNavigationMessageCtor);
109 SET(Type, static_cast<int32_t>(message.type));
110 SET(Svid, static_cast<int32_t>(message.svid));
111 SET(MessageId, static_cast<int32_t>(message.messageId));
112 SET(SubmessageId, static_cast<int32_t>(message.submessageId));
113 object.callSetter("setData", data, dataLength);
114 SET(Status, static_cast<int32_t>(message.status));
115
116 jobject navigationMessage = object.get();
117 env->CallVoidMethod(mCallbacksObj, method_reportNavigationMessages, navigationMessage);
118 checkAndClearExceptionFromCallback(env, __FUNCTION__);
119 env->DeleteLocalRef(navigationMessage);
120 return;
121 }
122
123 } // namespace android::gnss
124
125 #endif // _ANDROID_SERVER_GNSS_GNSSNAVIGATIONMESSAGECALLBACK_H