/* * Copyright (C) 2021 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef _ANDROID_SERVER_GNSS_GNSSNAVIGATIONMESSAGECALLBACK_H #define _ANDROID_SERVER_GNSS_GNSSNAVIGATIONMESSAGECALLBACK_H #pragma once #ifndef LOG_TAG #error LOG_TAG must be defined before including this file. #endif #include #include #include #include #include "Utils.h" #include "jni.h" namespace android::gnss { namespace { extern jclass class_gnssNavigationMessage; extern jmethodID method_reportNavigationMessages; extern jmethodID method_gnssNavigationMessageCtor; } // anonymous namespace void GnssNavigationMessage_class_init_once(JNIEnv* env, jclass clazz); class GnssNavigationMessageCallbackAidl : public hardware::gnss::BnGnssNavigationMessageCallback { public: GnssNavigationMessageCallbackAidl() {} android::binder::Status gnssNavigationMessageCb( const hardware::gnss::IGnssNavigationMessageCallback::GnssNavigationMessage& message) override; }; class GnssNavigationMessageCallbackHidl : public hardware::gnss::V1_0::IGnssNavigationMessageCallback { public: GnssNavigationMessageCallbackHidl() {} hardware::Return gnssNavigationMessageCb( const hardware::gnss::V1_0::IGnssNavigationMessageCallback::GnssNavigationMessage& message) override; }; class GnssNavigationMessageCallback { public: GnssNavigationMessageCallback() {} sp getAidl() { if (callbackAidl == nullptr) { callbackAidl = sp::make(); } return callbackAidl; } sp getHidl() { if (callbackHidl == nullptr) { callbackHidl = sp::make(); } return callbackHidl; } private: sp callbackAidl; sp callbackHidl; }; struct GnssNavigationMessageCallbackUtil { template static void gnssNavigationMessageCbImpl(const T& message); private: GnssNavigationMessageCallbackUtil() = delete; }; template void GnssNavigationMessageCallbackUtil::gnssNavigationMessageCbImpl(const T& message) { JNIEnv* env = getJniEnv(); size_t dataLength = message.data.size(); std::vector navigationData = message.data; uint8_t* data = &(navigationData[0]); if (dataLength == 0 || data == nullptr) { ALOGE("Invalid Navigation Message found: data=%p, length=%zd", data, dataLength); return; } JavaObject object(env, class_gnssNavigationMessage, method_gnssNavigationMessageCtor); SET(Type, static_cast(message.type)); SET(Svid, static_cast(message.svid)); SET(MessageId, static_cast(message.messageId)); SET(SubmessageId, static_cast(message.submessageId)); object.callSetter("setData", data, dataLength); SET(Status, static_cast(message.status)); jobject navigationMessage = object.get(); env->CallVoidMethod(mCallbacksObj, method_reportNavigationMessages, navigationMessage); checkAndClearExceptionFromCallback(env, __FUNCTION__); env->DeleteLocalRef(navigationMessage); return; } } // namespace android::gnss #endif // _ANDROID_SERVER_GNSS_GNSSNAVIGATIONMESSAGECALLBACK_H