1 /*
2  * Copyright (C) 2020 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 "ContextHubHal"
18 #define LOG_NDEBUG 0
19 
20 #include "generic_context_hub_v1_2.h"
21 
22 #include "context_hub_settings_util.h"
23 #include "permissions_util.h"
24 
25 #include <chrono>
26 #include <cinttypes>
27 #include <vector>
28 
29 #include <log/log.h>
30 #include <unistd.h>
31 
32 namespace android {
33 namespace hardware {
34 namespace contexthub {
35 namespace V1_2 {
36 namespace implementation {
37 
38 using ::android::chre::HostProtocolHost;
39 using ::android::hardware::Return;
40 using ::android::hardware::contexthub::common::implementation::getFbsSetting;
41 using ::android::hardware::contexthub::common::implementation::
42     getFbsSettingValue;
43 using ::android::hardware::contexthub::common::implementation::
44     kSupportedPermissions;
45 using ::android::hardware::contexthub::V1_X::implementation::
46     IContextHubCallbackWrapperBase;
47 using ::android::hardware::contexthub::V1_X::implementation::
48     IContextHubCallbackWrapperV1_2;
49 using ::flatbuffers::FlatBufferBuilder;
50 
51 using V1_0::ContextHub;
52 using V1_1::SettingValue;
53 using V1_2::IContexthub;
54 
55 // Aliased for consistency with the way these symbols are referenced in
56 // CHRE-side code
57 namespace fbs = ::chre::fbs;
58 
getHubs_1_2(IContexthub::getHubs_1_2_cb _hidl_cb)59 Return<void> GenericContextHubV1_2::getHubs_1_2(
60     IContexthub::getHubs_1_2_cb _hidl_cb) {
61   std::vector<ContextHub> retHubs;
62   getHubs([&retHubs](std::vector<ContextHub> hubs) { retHubs = hubs; });
63   _hidl_cb(retHubs, kSupportedPermissions);
64 
65   return Void();
66 }
67 
registerCallback_1_2(uint32_t hubId,const sp<IContexthubCallback> & cb)68 Return<Result> GenericContextHubV1_2::registerCallback_1_2(
69     uint32_t hubId, const sp<IContexthubCallback> &cb) {
70   sp<IContextHubCallbackWrapperBase> wrappedCallback;
71   if (cb != nullptr) {
72     wrappedCallback = new IContextHubCallbackWrapperV1_2(cb);
73   }
74   return registerCallbackCommon(hubId, wrappedCallback);
75 }
76 
onSettingChanged(V1_1::Setting setting,SettingValue newValue)77 Return<void> GenericContextHubV1_2::onSettingChanged(V1_1::Setting setting,
78                                                      SettingValue newValue) {
79   return onSettingChanged_1_2(reinterpret_cast<V1_2::Setting &>(setting),
80                               newValue);
81 }
82 
onSettingChanged_1_2(Setting setting,SettingValue newValue)83 Return<void> GenericContextHubV1_2::onSettingChanged_1_2(
84     Setting setting, SettingValue newValue) {
85   fbs::Setting fbsSetting;
86   fbs::SettingState fbsState;
87   if (getFbsSetting(setting, &fbsSetting) &&
88       getFbsSettingValue(newValue, &fbsState)) {
89     FlatBufferBuilder builder(64);
90     HostProtocolHost::encodeSettingChangeNotification(builder, fbsSetting,
91                                                       fbsState);
92     mClient.sendMessage(builder.GetBufferPointer(), builder.GetSize());
93   }
94 
95   return Void();
96 }
97 
98 }  // namespace implementation
99 }  // namespace V1_2
100 }  // namespace contexthub
101 }  // namespace hardware
102 }  // namespace android
103