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 #include "contexthub-impl/ContextHub.h"
18 
19 namespace aidl {
20 namespace android {
21 namespace hardware {
22 namespace contexthub {
23 
24 using ::ndk::ScopedAStatus;
25 
getContextHubs(std::vector<ContextHubInfo> * out_contextHubInfos)26 ScopedAStatus ContextHub::getContextHubs(std::vector<ContextHubInfo>* out_contextHubInfos) {
27     ContextHubInfo hub = {};
28     hub.name = "Mock Context Hub";
29     hub.vendor = "AOSP";
30     hub.toolchain = "n/a";
31     hub.id = kMockHubId;
32     hub.peakMips = 1;
33     hub.maxSupportedMessageLengthBytes = 4096;
34     hub.chrePlatformId = UINT64_C(0x476f6f6754000000);
35     hub.chreApiMajorVersion = 1;
36     hub.chreApiMinorVersion = 6;
37 
38     out_contextHubInfos->push_back(hub);
39 
40     return ndk::ScopedAStatus::ok();
41 }
42 
43 // We don't expose any nanoapps for the default impl, therefore all nanoapp-related APIs fail.
loadNanoapp(int32_t,const NanoappBinary &,int32_t)44 ScopedAStatus ContextHub::loadNanoapp(int32_t /* in_contextHubId */,
45                                       const NanoappBinary& /* in_appBinary */,
46                                       int32_t /* in_transactionId */) {
47     return ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
48 }
49 
unloadNanoapp(int32_t,int64_t,int32_t)50 ScopedAStatus ContextHub::unloadNanoapp(int32_t /* in_contextHubId */, int64_t /* in_appId */,
51                                         int32_t /* in_transactionId */) {
52     return ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
53 }
54 
disableNanoapp(int32_t,int64_t,int32_t)55 ScopedAStatus ContextHub::disableNanoapp(int32_t /* in_contextHubId */, int64_t /* in_appId */,
56                                          int32_t /* in_transactionId */) {
57     return ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
58 }
59 
enableNanoapp(int32_t,int64_t,int32_t)60 ScopedAStatus ContextHub::enableNanoapp(int32_t /* in_contextHubId */, int64_t /* in_appId */,
61                                         int32_t /* in_transactionId */) {
62     return ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
63 }
64 
onSettingChanged(Setting,bool)65 ScopedAStatus ContextHub::onSettingChanged(Setting /* in_setting */, bool /*in_enabled */) {
66     return ndk::ScopedAStatus::ok();
67 }
68 
queryNanoapps(int32_t in_contextHubId)69 ScopedAStatus ContextHub::queryNanoapps(int32_t in_contextHubId) {
70     if (in_contextHubId == kMockHubId && mCallback != nullptr) {
71         std::vector<NanoappInfo> nanoapps;
72         mCallback->handleNanoappInfo(nanoapps);
73         return ndk::ScopedAStatus::ok();
74     } else {
75         return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
76     }
77 }
78 
getPreloadedNanoappIds(int32_t,std::vector<int64_t> * out_preloadedNanoappIds)79 ScopedAStatus ContextHub::getPreloadedNanoappIds(int32_t /* in_contextHubId */,
80                                                  std::vector<int64_t>* out_preloadedNanoappIds) {
81     if (out_preloadedNanoappIds == nullptr) {
82         return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
83     }
84 
85     for (uint64_t i = 0; i < 10; ++i) {
86         out_preloadedNanoappIds->push_back(i);
87     }
88     return ndk::ScopedAStatus::ok();
89 }
90 
onNanSessionStateChanged(const NanSessionStateUpdate &)91 ScopedAStatus ContextHub::onNanSessionStateChanged(const NanSessionStateUpdate& /*in_update*/) {
92     return ndk::ScopedAStatus::ok();
93 }
94 
registerCallback(int32_t in_contextHubId,const std::shared_ptr<IContextHubCallback> & in_cb)95 ScopedAStatus ContextHub::registerCallback(int32_t in_contextHubId,
96                                            const std::shared_ptr<IContextHubCallback>& in_cb) {
97     if (in_contextHubId == kMockHubId) {
98         mCallback = in_cb;
99         return ndk::ScopedAStatus::ok();
100     } else {
101         return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
102     }
103 }
104 
sendMessageToHub(int32_t in_contextHubId,const ContextHubMessage &)105 ScopedAStatus ContextHub::sendMessageToHub(int32_t in_contextHubId,
106                                            const ContextHubMessage& /* in_message */) {
107     if (in_contextHubId == kMockHubId) {
108         // Return true here to indicate that the HAL has accepted the message.
109         // Successful delivery of the message to a nanoapp should be handled at
110         // a higher level protocol.
111         return ndk::ScopedAStatus::ok();
112     } else {
113         return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
114     }
115 }
116 
setTestMode(bool)117 ScopedAStatus ContextHub::setTestMode(bool /* enable */) {
118     return ndk::ScopedAStatus::ok();
119 }
120 
onHostEndpointConnected(const HostEndpointInfo & in_info)121 ScopedAStatus ContextHub::onHostEndpointConnected(const HostEndpointInfo& in_info) {
122     mConnectedHostEndpoints.insert(in_info.hostEndpointId);
123 
124     return ndk::ScopedAStatus::ok();
125 }
126 
onHostEndpointDisconnected(char16_t in_hostEndpointId)127 ScopedAStatus ContextHub::onHostEndpointDisconnected(char16_t in_hostEndpointId) {
128     if (mConnectedHostEndpoints.count(in_hostEndpointId) > 0) {
129         mConnectedHostEndpoints.erase(in_hostEndpointId);
130     }
131 
132     return ndk::ScopedAStatus::ok();
133 }
134 
135 }  // namespace contexthub
136 }  // namespace hardware
137 }  // namespace android
138 }  // namespace aidl
139