diff --git a/contexthub/1.1/default/Contexthub.cpp b/contexthub/1.1/default/Contexthub.cpp index e7fde84058..2d4fbaee24 100644 --- a/contexthub/1.1/default/Contexthub.cpp +++ b/contexthub/1.1/default/Contexthub.cpp @@ -23,10 +23,29 @@ namespace contexthub { namespace V1_1 { namespace implementation { +using ::android::hardware::contexthub::V1_0::Result; + Return Contexthub::onSettingChanged(Setting /*setting*/, SettingValue /*newValue*/) { return Void(); } +Return Contexthub::registerCallback(uint32_t hubId, const sp& cb) { + if (hubId == kMockHubId) { + mCallback = cb; + return Result::OK; + } + return Result::BAD_PARAMS; +} + +Return Contexthub::queryApps(uint32_t hubId) { + if (hubId == kMockHubId && mCallback != nullptr) { + std::vector nanoapps; + mCallback->handleAppsInfo(nanoapps); + return Result::OK; + } + return Result::BAD_PARAMS; +} + } // namespace implementation } // namespace V1_1 } // namespace contexthub diff --git a/contexthub/1.1/default/Contexthub.h b/contexthub/1.1/default/Contexthub.h index 1468fcfc0e..648749e27a 100644 --- a/contexthub/1.1/default/Contexthub.h +++ b/contexthub/1.1/default/Contexthub.h @@ -27,9 +27,19 @@ namespace implementation { class Contexthub : public ::android::hardware::contexthub::V1_X::implementation::ContextHub { + using Result = ::android::hardware::contexthub::V1_0::Result; + public: + // Methods from V1_0::IContexthub + Return registerCallback(uint32_t hubId, const sp& cb) override; + + Return queryApps(uint32_t hubId) override; + // Methods from V1_1::IContexthub Return onSettingChanged(Setting setting, SettingValue newValue) override; + + private: + sp mCallback; }; } // namespace implementation diff --git a/contexthub/1.2/Android.bp b/contexthub/1.2/Android.bp index e81948234a..9722a97ee6 100644 --- a/contexthub/1.2/Android.bp +++ b/contexthub/1.2/Android.bp @@ -6,6 +6,7 @@ hidl_interface { srcs: [ "types.hal", "IContexthub.hal", + "IContexthubCallback.hal", ], interfaces: [ "android.hardware.contexthub@1.0", diff --git a/contexthub/1.2/IContexthub.hal b/contexthub/1.2/IContexthub.hal index 819fc1d6e1..3488b7446c 100644 --- a/contexthub/1.2/IContexthub.hal +++ b/contexthub/1.2/IContexthub.hal @@ -16,10 +16,40 @@ package android.hardware.contexthub@1.2; +import @1.0::Result; import @1.1::IContexthub; import @1.1::SettingValue; +import IContexthubCallback; interface IContexthub extends @1.1::IContexthub { + /** + * Register a callback for the HAL implementation to send asynchronous + * messages to the service from a context hub. There can be a maximum of + * one callback registered with the HAL. A call to this function when a + * callback has already been registered must override the previous + * registration. + * + * @param hubId identifier for the hub + * @param callback an implementation of the IContextHubCallbacks + * + * @return result OK on success + * BAD_VALUE if parameters are not valid + * + */ + registerCallback_1_2(uint32_t hubId, IContexthubCallback cb) generates (Result result); + + /** + * Send a message to a hub + * + * @param hubId identifier for hub to send message to + * @param msg message to be sent + * + * @return result OK if successful, error code otherwise + * BAD_VALUE if parameters are not valid + * TRANSACTION_FAILED if message send failed + */ + sendMessageToHub_1_2(uint32_t hubId, ContextHubMsg msg) generates (Result result); + /** * Notification sent by the framework to indicate that the user * has changed a setting. diff --git a/contexthub/1.2/IContexthubCallback.hal b/contexthub/1.2/IContexthubCallback.hal new file mode 100644 index 0000000000..0236160305 --- /dev/null +++ b/contexthub/1.2/IContexthubCallback.hal @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2020 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. + */ + +package android.hardware.contexthub@1.2; + +import @1.0::IContexthubCallback; + +interface IContexthubCallback extends @1.0::IContexthubCallback { + /** + * This callback is passed by the Contexthub service to the HAL + * implementation to allow the HAL to send asynchronous messages back + * to the service and registered clients of the ContextHub service. + * + * @param msg message that should be delivered to host app clients + * + */ + handleClientMsg_1_2(ContextHubMsg msg); + + /** + * This callback is passed by the Contexthub service to the HAL + * implementation to allow the HAL to send information about the + * currently loaded and active nanoapps on the hub. + * + * @param appInfo vector of HubAppinfo structure for each nanoApp + * on the hub that can be enabled, disabled and + * unloaded by the service. Any nanoApps that cannot + * be controlled by the service must not be reported. + * All nanoApps that can be controlled by the service + * must be reported. + */ + handleAppsInfo_1_2(vec appInfo); +}; diff --git a/contexthub/1.2/default/Android.bp b/contexthub/1.2/default/Android.bp index 49b54fc007..0a31325811 100644 --- a/contexthub/1.2/default/Android.bp +++ b/contexthub/1.2/default/Android.bp @@ -41,6 +41,7 @@ cc_binary { ], header_libs: [ "android.hardware.contexthub@1.X-common-impl", + "android.hardware.contexthub@1.X-common-utils", ], vintf_fragments: ["android.hardware.contexthub@1.2.xml"], } diff --git a/contexthub/1.2/default/Contexthub.cpp b/contexthub/1.2/default/Contexthub.cpp index d7ac7bf30e..db0c5bc3df 100644 --- a/contexthub/1.2/default/Contexthub.cpp +++ b/contexthub/1.2/default/Contexthub.cpp @@ -23,6 +23,43 @@ namespace contexthub { namespace V1_2 { namespace implementation { +using ::android::hardware::contexthub::V1_0::Result; +using ::android::hardware::contexthub::V1_X::implementation::IContextHubCallbackWrapperV1_0; +using ::android::hardware::contexthub::V1_X::implementation::IContextHubCallbackWrapperV1_2; + +Return Contexthub::registerCallback(uint32_t hubId, + const sp& cb) { + if (hubId == kMockHubId) { + mCallback = new IContextHubCallbackWrapperV1_0(cb); + return Result::OK; + } + return Result::BAD_PARAMS; +} + +Return Contexthub::queryApps(uint32_t hubId) { + if (hubId == kMockHubId && mCallback != nullptr) { + std::vector nanoapps; + mCallback->handleAppsInfo(nanoapps); + return Result::OK; + } + return Result::BAD_PARAMS; +} + +Return Contexthub::registerCallback_1_2(uint32_t hubId, + const sp& cb) { + if (hubId == kMockHubId) { + mCallback = new IContextHubCallbackWrapperV1_2(cb); + return Result::OK; + } + return Result::BAD_PARAMS; +} + +// We don't expose any nanoapps, therefore all nanoapp-related API calls return with BAD_PARAMS +Return Contexthub::sendMessageToHub_1_2(uint32_t /* hubId */, + const ContextHubMsg& /* msg */) { + return Result::BAD_PARAMS; +} + Return Contexthub::onSettingChanged(SettingV1_1 /*setting*/, SettingValue /*newValue*/) { return Void(); } diff --git a/contexthub/1.2/default/Contexthub.h b/contexthub/1.2/default/Contexthub.h index d2f8d69afc..8b89824d6b 100644 --- a/contexthub/1.2/default/Contexthub.h +++ b/contexthub/1.2/default/Contexthub.h @@ -16,6 +16,7 @@ #pragma once #include "ContextHub.h" +#include "IContextHubCallbackWrapper.h" #include @@ -27,15 +28,34 @@ namespace implementation { class Contexthub : public ::android::hardware::contexthub::V1_X::implementation::ContextHub { + using ContextHubMsg = ::android::hardware::contexthub::V1_2::ContextHubMsg; + using IContexthubCallback = ::android::hardware::contexthub::V1_2::IContexthubCallback; + using IContextHubCallbackWrapperBase = + ::android::hardware::contexthub::V1_X::implementation::IContextHubCallbackWrapperBase; + using Result = ::android::hardware::contexthub::V1_0::Result; using SettingValue = ::android::hardware::contexthub::V1_1::SettingValue; using SettingV1_1 = ::android::hardware::contexthub::V1_1::Setting; public: + // Methods from V1_0::IContexthub + Return registerCallback(uint32_t hubId, + const sp& cb) override; + + Return queryApps(uint32_t hubId) override; + // Methods from V1_1::IContexthub Return onSettingChanged(SettingV1_1 setting, SettingValue newValue) override; // Methods from V1_2::IContexthub Return onSettingChanged_1_2(Setting setting, SettingValue newValue) override; + + Return registerCallback_1_2(uint32_t hubId, + const sp& cb) override; + + Return sendMessageToHub_1_2(uint32_t hubId, const ContextHubMsg& msg) override; + + private: + sp mCallback; }; } // namespace implementation diff --git a/contexthub/1.2/types.hal b/contexthub/1.2/types.hal index 38f9f7af55..e6c8acc57d 100644 --- a/contexthub/1.2/types.hal +++ b/contexthub/1.2/types.hal @@ -16,6 +16,8 @@ package android.hardware.contexthub@1.2; +import @1.0::ContextHubMsg; +import @1.0::HubAppInfo; import @1.1::Setting; /** @@ -32,3 +34,36 @@ enum Setting : @1.1::Setting { WIFI_AVAILABLE, AIRPLANE_MODE, }; + +struct ContextHubMsg { + @1.0::ContextHubMsg msg_1_0; + + /** + * The list of Android permissions that the sender of this message has at + * the time the message was sent. + * + * The HAL MUST drop messages to nanoapps if this list of permissions is not + * a superset of those of the receiving nanoapp(s). + * + * The framework MUST drop messages to host apps that don't have a superset + * of the permissions that the sending nanoapp is using. + */ + vec permissions; +}; + +struct HubAppInfo { + @1.0::HubAppInfo info_1_0; + + /** + * The list of Android permissions used by this nanoapp. This list MUST + * correspond to the permissions required for an equivalent Android app to + * sample similar signals through the Android framework. + * + * For example, if a nanoapp used location-based signals, the permissions + * list MUST contains android.permission.ACCESS_FINE_LOCATION and + * android.permission.ACCESS_BACKGROUND_LOCATION. If it were to also list to + * audio data, it would require adding android.permission.RECORD_AUDIO to + * this list. + */ + vec permissions; +}; diff --git a/contexthub/common/default/1.X/ContextHub.h b/contexthub/common/default/1.X/ContextHub.h index 73d06319dd..00f74afa1b 100644 --- a/contexthub/common/default/1.X/ContextHub.h +++ b/contexthub/common/default/1.X/ContextHub.h @@ -60,14 +60,6 @@ struct ContextHub : public IContextHubInterface { return Void(); } - Return registerCallback(uint32_t hubId, const sp& cb) override { - if (hubId == kMockHubId) { - mCallback = cb; - return Result::OK; - } - return Result::BAD_PARAMS; - } - // We don't expose any nanoapps, therefore all nanoapp-related API calls return with BAD_PARAMS Return sendMessageToHub(uint32_t /*hubId*/, const ContextHubMsg& /*msg*/) override { return Result::BAD_PARAMS; @@ -93,19 +85,8 @@ struct ContextHub : public IContextHubInterface { return Result::BAD_PARAMS; } - Return queryApps(uint32_t hubId) override { - if (hubId == kMockHubId && mCallback != nullptr) { - std::vector nanoapps; - mCallback->handleAppsInfo(nanoapps); - return Result::OK; - } - return Result::BAD_PARAMS; - } - - private: + protected: static constexpr uint32_t kMockHubId = 0; - - sp mCallback; }; } // namespace implementation diff --git a/contexthub/common/default/1.X/OWNERS b/contexthub/common/default/1.X/OWNERS new file mode 100644 index 0000000000..90c233030e --- /dev/null +++ b/contexthub/common/default/1.X/OWNERS @@ -0,0 +1,3 @@ +arthuri@google.com +bduddie@google.com +stange@google.com diff --git a/contexthub/common/default/1.X/utils/Android.bp b/contexthub/common/default/1.X/utils/Android.bp new file mode 100644 index 0000000000..c74b647413 --- /dev/null +++ b/contexthub/common/default/1.X/utils/Android.bp @@ -0,0 +1,30 @@ +// +// Copyright (C) 2020 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. + +cc_library_headers { + name: "android.hardware.contexthub@1.X-common-utils", + vendor_available: true, + defaults: ["hidl_defaults"], + export_include_dirs: ["."], + shared_libs: [ + "android.hardware.contexthub@1.0", + "android.hardware.contexthub@1.1", + "android.hardware.contexthub@1.2", + "libbinder", + "libcutils", + "libhidlbase", + "libutils", + ], +} diff --git a/contexthub/common/default/1.X/utils/IContextHubCallbackWrapper.h b/contexthub/common/default/1.X/utils/IContextHubCallbackWrapper.h new file mode 100644 index 0000000000..df78438750 --- /dev/null +++ b/contexthub/common/default/1.X/utils/IContextHubCallbackWrapper.h @@ -0,0 +1,123 @@ +/* + * Copyright (C) 2020 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_HARDWARE_CONTEXTHUB_V1_X_ICONTEXTHUBCALLBACKWRAPPER_H +#define ANDROID_HARDWARE_CONTEXTHUB_V1_X_ICONTEXTHUBCALLBACKWRAPPER_H + +#include "android/hardware/contexthub/1.0/IContexthub.h" +#include "android/hardware/contexthub/1.0/IContexthubCallback.h" +#include "android/hardware/contexthub/1.0/types.h" +#include "android/hardware/contexthub/1.2/IContexthubCallback.h" +#include "android/hardware/contexthub/1.2/types.h" + +#include + +#include + +namespace android { +namespace hardware { +namespace contexthub { +namespace V1_X { +namespace implementation { + +inline V1_0::ContextHubMsg convertToOldMsg(V1_2::ContextHubMsg msg) { + return msg.msg_1_0; +} + +inline hidl_vec convertToOldAppInfo(hidl_vec appInfos) { + hidl_vec convertedInfo(appInfos.size()); + for (int i = 0; i < appInfos.size(); ++i) { + convertedInfo[i] = appInfos[i].info_1_0; + } + + return convertedInfo; +} + +/** + * The IContexthubCallback classes below abstract away the common logic between both the V1.0, and + * V1.2 versions of the Contexthub HAL callback interface. This allows users of these classes to + * only care about the HAL version at init time and then interact with either version of the + * callback without worrying about the class type by utilizing the base class. + */ +class IContextHubCallbackWrapperBase : public VirtualLightRefBase { + public: + virtual Return handleClientMsg(V1_2::ContextHubMsg msg) = 0; + + virtual Return handleTxnResult(uint32_t txnId, V1_0::TransactionResult result) = 0; + + virtual Return handleHubEvent(V1_0::AsyncEventType evt) = 0; + + virtual Return handleAppAbort(uint64_t appId, uint32_t abortCode) = 0; + + virtual Return handleAppsInfo(hidl_vec appInfo) = 0; +}; + +template +class ContextHubCallbackWrapper : public IContextHubCallbackWrapperBase { + public: + ContextHubCallbackWrapper(sp callback) : mCallback(callback){}; + + virtual Return handleClientMsg(V1_2::ContextHubMsg msg) override { + return mCallback->handleClientMsg(convertToOldMsg(msg)); + } + + virtual Return handleTxnResult(uint32_t txnId, V1_0::TransactionResult result) override { + return mCallback->handleTxnResult(txnId, result); + } + + virtual Return handleHubEvent(V1_0::AsyncEventType evt) override { + return mCallback->handleHubEvent(evt); + } + + virtual Return handleAppAbort(uint64_t appId, uint32_t abortCode) override { + return mCallback->handleAppAbort(appId, abortCode); + } + + virtual Return handleAppsInfo(hidl_vec appInfo) override { + return mCallback->handleAppsInfo(convertToOldAppInfo(appInfo)); + } + + protected: + sp mCallback; +}; + +class IContextHubCallbackWrapperV1_0 : public ContextHubCallbackWrapper { + public: + IContextHubCallbackWrapperV1_0(sp callback) + : ContextHubCallbackWrapper(callback){}; +}; + +class IContextHubCallbackWrapperV1_2 : public ContextHubCallbackWrapper { + public: + IContextHubCallbackWrapperV1_2(sp callback) + : ContextHubCallbackWrapper(callback){}; + + Return handleClientMsg(V1_2::ContextHubMsg msg) override { + return mCallback->handleClientMsg_1_2(msg); + } + + Return handleAppsInfo(hidl_vec appInfo) override { + return mCallback->handleAppsInfo_1_2(appInfo); + } +}; + +} // namespace implementation +} // namespace V1_X +} // namespace contexthub +} // namespace hardware +} // namespace android + +#endif // ANDROID_HARDWARE_CONTEXTHUB_V1_X_ICONTEXTHUBCALLBACKWRAPPER_H \ No newline at end of file diff --git a/current.txt b/current.txt index 8623fc001b..0b88a7a835 100644 --- a/current.txt +++ b/current.txt @@ -787,6 +787,4 @@ b9fbb6e2e061ed0960939d48b785e9700210add1f13ed32ecd688d0f1ca20ef7 android.hardwar # HALs released in Android S # NOTE: waiting to freeze HALs until later in the release -# NOTE: new HALs are recommended to be in AIDL -6e64b33f1b720b66b0deb5e08dee37a99deaa94e2e9ebf7806703cabab56e21d android.hardware.contexthub@1.2::IContexthub -3fb83f4539cab2c7bf9fdbecf7265d1c1dd6e8de9694046fe512b493c127ccea android.hardware.contexthub@1.2::types +# NOTE: new HALs are recommended to be in AIDL \ No newline at end of file