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