Arthur Ishiguro | a257b78 | 2021-08-04 10:40:29 -0700 | [diff] [blame] | 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" |
Matthew Sedam | 0ec290c | 2024-12-11 11:18:15 -0800 | [diff] [blame] | 18 | #include "aidl/android/hardware/contexthub/IContextHubCallback.h" |
Arthur Ishiguro | a257b78 | 2021-08-04 10:40:29 -0700 | [diff] [blame] | 19 | |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 20 | #ifndef LOG_TAG |
| 21 | #define LOG_TAG "CHRE" |
| 22 | #endif |
| 23 | |
| 24 | #include <inttypes.h> |
| 25 | #include <log/log.h> |
Matthew Sedam | 0ec290c | 2024-12-11 11:18:15 -0800 | [diff] [blame] | 26 | #include <optional> |
| 27 | #include <thread> |
Arthur Ishiguro | a257b78 | 2021-08-04 10:40:29 -0700 | [diff] [blame] | 28 | |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 29 | using ::ndk::ScopedAStatus; |
| 30 | |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 31 | namespace aidl::android::hardware::contexthub { |
| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | constexpr uint64_t kMockVendorHubId = 0x1234567812345678; |
| 36 | constexpr uint64_t kMockVendorHub2Id = 0x0EADBEEFDEADBEEF; |
| 37 | |
| 38 | // Mock endpoints for the default implementation. |
| 39 | // These endpoints just echo back any messages sent to them. |
| 40 | constexpr size_t kMockEndpointCount = 4; |
| 41 | const EndpointInfo kMockEndpointInfos[kMockEndpointCount] = { |
| 42 | { |
| 43 | .id = {.hubId = kMockVendorHubId, .id = UINT64_C(0x1)}, |
| 44 | .type = EndpointInfo::EndpointType::GENERIC, |
| 45 | .name = "Mock Endpoint 1", |
| 46 | .version = 1, |
| 47 | }, |
| 48 | { |
| 49 | .id = {.hubId = kMockVendorHubId, .id = UINT64_C(0x2)}, |
| 50 | .type = EndpointInfo::EndpointType::GENERIC, |
| 51 | .name = "Mock Endpoint 2", |
| 52 | .version = 2, |
| 53 | }, |
| 54 | { |
| 55 | .id = {.hubId = kMockVendorHub2Id, .id = UINT64_C(0x1)}, |
| 56 | .type = EndpointInfo::EndpointType::GENERIC, |
| 57 | .name = "Mock Endpoint 3", |
| 58 | .version = 1, |
| 59 | }, |
| 60 | { |
| 61 | .id = {.hubId = kMockVendorHub2Id, .id = UINT64_C(0x2)}, |
| 62 | .type = EndpointInfo::EndpointType::GENERIC, |
| 63 | .name = "Mock Endpoint 4", |
| 64 | .version = 2, |
| 65 | }, |
| 66 | }; |
| 67 | |
Matthew Sedam | 0ec290c | 2024-12-11 11:18:15 -0800 | [diff] [blame] | 68 | //! Mutex used to ensure callbacks are called after the initial function returns. |
| 69 | std::mutex gCallbackMutex; |
| 70 | |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 71 | } // anonymous namespace |
| 72 | |
Arthur Ishiguro | 0d6009e | 2024-12-24 16:31:20 -0800 | [diff] [blame] | 73 | ScopedAStatus ContextHub::getContextHubs(std::vector<ContextHubInfo>* /* out_contextHubInfos */) { |
Matthew Sedam | adfd557 | 2023-11-21 05:53:00 -0800 | [diff] [blame] | 74 | return ScopedAStatus::ok(); |
Arthur Ishiguro | a257b78 | 2021-08-04 10:40:29 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Arthur Ishiguro | 94e1aa2 | 2021-10-26 17:25:19 +0000 | [diff] [blame] | 77 | // We don't expose any nanoapps for the default impl, therefore all nanoapp-related APIs fail. |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 78 | ScopedAStatus ContextHub::loadNanoapp(int32_t /* in_contextHubId */, |
| 79 | const NanoappBinary& /* in_appBinary */, |
| 80 | int32_t /* in_transactionId */) { |
| 81 | return ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 82 | } |
| 83 | |
| 84 | ScopedAStatus ContextHub::unloadNanoapp(int32_t /* in_contextHubId */, int64_t /* in_appId */, |
| 85 | int32_t /* in_transactionId */) { |
| 86 | return ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 87 | } |
| 88 | |
| 89 | ScopedAStatus ContextHub::disableNanoapp(int32_t /* in_contextHubId */, int64_t /* in_appId */, |
| 90 | int32_t /* in_transactionId */) { |
| 91 | return ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 92 | } |
| 93 | |
| 94 | ScopedAStatus ContextHub::enableNanoapp(int32_t /* in_contextHubId */, int64_t /* in_appId */, |
| 95 | int32_t /* in_transactionId */) { |
| 96 | return ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 97 | } |
| 98 | |
| 99 | ScopedAStatus ContextHub::onSettingChanged(Setting /* in_setting */, bool /*in_enabled */) { |
Matthew Sedam | adfd557 | 2023-11-21 05:53:00 -0800 | [diff] [blame] | 100 | return ScopedAStatus::ok(); |
Arthur Ishiguro | a257b78 | 2021-08-04 10:40:29 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 103 | ScopedAStatus ContextHub::queryNanoapps(int32_t in_contextHubId) { |
Arthur Ishiguro | 94e1aa2 | 2021-10-26 17:25:19 +0000 | [diff] [blame] | 104 | if (in_contextHubId == kMockHubId && mCallback != nullptr) { |
| 105 | std::vector<NanoappInfo> nanoapps; |
| 106 | mCallback->handleNanoappInfo(nanoapps); |
Matthew Sedam | adfd557 | 2023-11-21 05:53:00 -0800 | [diff] [blame] | 107 | return ScopedAStatus::ok(); |
Arthur Ishiguro | 94e1aa2 | 2021-10-26 17:25:19 +0000 | [diff] [blame] | 108 | } else { |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 109 | return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
Arthur Ishiguro | 94e1aa2 | 2021-10-26 17:25:19 +0000 | [diff] [blame] | 110 | } |
Arthur Ishiguro | a257b78 | 2021-08-04 10:40:29 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Matthew Sedam | d70f84d | 2023-03-06 18:34:24 +0000 | [diff] [blame] | 113 | ScopedAStatus ContextHub::getPreloadedNanoappIds(int32_t /* in_contextHubId */, |
| 114 | std::vector<int64_t>* out_preloadedNanoappIds) { |
Arthur Ishiguro | fd5e65c | 2022-11-08 16:49:47 +0000 | [diff] [blame] | 115 | if (out_preloadedNanoappIds == nullptr) { |
| 116 | return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 117 | } |
| 118 | |
| 119 | for (uint64_t i = 0; i < 10; ++i) { |
| 120 | out_preloadedNanoappIds->push_back(i); |
| 121 | } |
Matthew Sedam | adfd557 | 2023-11-21 05:53:00 -0800 | [diff] [blame] | 122 | return ScopedAStatus::ok(); |
Arthur Ishiguro | fd5e65c | 2022-11-08 16:49:47 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Anthony Stange | 7fba100 | 2023-03-02 21:45:20 +0000 | [diff] [blame] | 125 | ScopedAStatus ContextHub::onNanSessionStateChanged(const NanSessionStateUpdate& /*in_update*/) { |
Matthew Sedam | adfd557 | 2023-11-21 05:53:00 -0800 | [diff] [blame] | 126 | return ScopedAStatus::ok(); |
Anthony Stange | 7344af9 | 2022-12-22 14:21:31 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 129 | ScopedAStatus ContextHub::registerCallback(int32_t in_contextHubId, |
| 130 | const std::shared_ptr<IContextHubCallback>& in_cb) { |
Arthur Ishiguro | 94e1aa2 | 2021-10-26 17:25:19 +0000 | [diff] [blame] | 131 | if (in_contextHubId == kMockHubId) { |
| 132 | mCallback = in_cb; |
Matthew Sedam | adfd557 | 2023-11-21 05:53:00 -0800 | [diff] [blame] | 133 | return ScopedAStatus::ok(); |
Arthur Ishiguro | 94e1aa2 | 2021-10-26 17:25:19 +0000 | [diff] [blame] | 134 | } else { |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 135 | return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
Arthur Ishiguro | 94e1aa2 | 2021-10-26 17:25:19 +0000 | [diff] [blame] | 136 | } |
Arthur Ishiguro | a257b78 | 2021-08-04 10:40:29 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 139 | ScopedAStatus ContextHub::sendMessageToHub(int32_t in_contextHubId, |
| 140 | const ContextHubMessage& /* in_message */) { |
Arthur Ishiguro | 94e1aa2 | 2021-10-26 17:25:19 +0000 | [diff] [blame] | 141 | if (in_contextHubId == kMockHubId) { |
| 142 | // Return true here to indicate that the HAL has accepted the message. |
| 143 | // Successful delivery of the message to a nanoapp should be handled at |
| 144 | // a higher level protocol. |
Matthew Sedam | adfd557 | 2023-11-21 05:53:00 -0800 | [diff] [blame] | 145 | return ScopedAStatus::ok(); |
Arthur Ishiguro | 94e1aa2 | 2021-10-26 17:25:19 +0000 | [diff] [blame] | 146 | } else { |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 147 | return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
Arthur Ishiguro | 94e1aa2 | 2021-10-26 17:25:19 +0000 | [diff] [blame] | 148 | } |
Arthur Ishiguro | a257b78 | 2021-08-04 10:40:29 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 151 | ScopedAStatus ContextHub::setTestMode(bool enable) { |
| 152 | if (enable) { |
| 153 | std::unique_lock<std::mutex> lock(mEndpointMutex); |
| 154 | mEndpoints.clear(); |
| 155 | mEndpointSessions.clear(); |
| 156 | mEndpointCallback = nullptr; |
| 157 | } |
Matthew Sedam | adfd557 | 2023-11-21 05:53:00 -0800 | [diff] [blame] | 158 | return ScopedAStatus::ok(); |
Matthew Sedam | c8ce4d5 | 2023-01-09 20:18:21 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 161 | ScopedAStatus ContextHub::onHostEndpointConnected(const HostEndpointInfo& in_info) { |
Arthur Ishiguro | 065a9a5 | 2021-11-19 00:24:45 +0000 | [diff] [blame] | 162 | mConnectedHostEndpoints.insert(in_info.hostEndpointId); |
| 163 | |
Matthew Sedam | adfd557 | 2023-11-21 05:53:00 -0800 | [diff] [blame] | 164 | return ScopedAStatus::ok(); |
Arthur Ishiguro | 065a9a5 | 2021-11-19 00:24:45 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 167 | ScopedAStatus ContextHub::onHostEndpointDisconnected(char16_t in_hostEndpointId) { |
Arthur Ishiguro | 065a9a5 | 2021-11-19 00:24:45 +0000 | [diff] [blame] | 168 | if (mConnectedHostEndpoints.count(in_hostEndpointId) > 0) { |
| 169 | mConnectedHostEndpoints.erase(in_hostEndpointId); |
Arthur Ishiguro | 065a9a5 | 2021-11-19 00:24:45 +0000 | [diff] [blame] | 170 | } |
Arthur Ishiguro | bb1d8bf | 2022-07-06 15:29:13 +0000 | [diff] [blame] | 171 | |
Matthew Sedam | adfd557 | 2023-11-21 05:53:00 -0800 | [diff] [blame] | 172 | return ScopedAStatus::ok(); |
Arthur Ishiguro | 065a9a5 | 2021-11-19 00:24:45 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Matthew Sedam | adfd557 | 2023-11-21 05:53:00 -0800 | [diff] [blame] | 175 | ScopedAStatus ContextHub::sendMessageDeliveryStatusToHub( |
| 176 | int32_t /* in_contextHubId */, |
| 177 | const MessageDeliveryStatus& /* in_messageDeliveryStatus */) { |
| 178 | return ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 179 | } |
| 180 | |
Yifei Zhang | 51eba2e | 2024-10-24 15:11:54 -0700 | [diff] [blame] | 181 | ScopedAStatus ContextHub::getHubs(std::vector<HubInfo>* _aidl_return) { |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 182 | if (_aidl_return == nullptr) { |
| 183 | return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 184 | } |
| 185 | |
Yifei Zhang | 51eba2e | 2024-10-24 15:11:54 -0700 | [diff] [blame] | 186 | VendorHubInfo vendorHub = {}; |
| 187 | vendorHub.name = "Mock Vendor Hub"; |
| 188 | vendorHub.version = 42; |
| 189 | |
Arthur Ishiguro | 0d6009e | 2024-12-24 16:31:20 -0800 | [diff] [blame] | 190 | HubInfo hubInfo1 = {}; |
| 191 | hubInfo1.hubId = kMockVendorHubId; |
| 192 | hubInfo1.hubDetails = |
Yifei Zhang | 51eba2e | 2024-10-24 15:11:54 -0700 | [diff] [blame] | 193 | HubInfo::HubDetails::make<HubInfo::HubDetails::Tag::vendorHubInfo>(vendorHub); |
| 194 | |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 195 | VendorHubInfo vendorHub2 = {}; |
| 196 | vendorHub2.name = "Mock Vendor Hub 2"; |
| 197 | vendorHub2.version = 24; |
| 198 | |
Arthur Ishiguro | 0d6009e | 2024-12-24 16:31:20 -0800 | [diff] [blame] | 199 | HubInfo hubInfo2 = {}; |
| 200 | hubInfo2.hubId = kMockVendorHub2Id; |
| 201 | hubInfo2.hubDetails = |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 202 | HubInfo::HubDetails::make<HubInfo::HubDetails::Tag::vendorHubInfo>(vendorHub2); |
| 203 | |
Yifei Zhang | 51eba2e | 2024-10-24 15:11:54 -0700 | [diff] [blame] | 204 | _aidl_return->push_back(hubInfo1); |
| 205 | _aidl_return->push_back(hubInfo2); |
| 206 | |
| 207 | return ScopedAStatus::ok(); |
| 208 | }; |
| 209 | |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 210 | ScopedAStatus ContextHub::getEndpoints(std::vector<EndpointInfo>* _aidl_return) { |
| 211 | if (_aidl_return == nullptr) { |
| 212 | return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 213 | } |
| 214 | |
| 215 | Service echoService; |
| 216 | echoService.format = Service::RpcFormat::CUSTOM; |
Arthur Ishiguro | 3908771 | 2024-12-13 09:48:13 -0800 | [diff] [blame] | 217 | echoService.serviceDescriptor = "android.hardware.contexthub.test.EchoService"; |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 218 | echoService.majorVersion = 1; |
| 219 | echoService.minorVersion = 0; |
| 220 | |
| 221 | for (const EndpointInfo& endpoint : kMockEndpointInfos) { |
| 222 | EndpointInfo endpointWithService(endpoint); |
| 223 | endpointWithService.services.push_back(echoService); |
| 224 | _aidl_return->push_back(std::move(endpointWithService)); |
| 225 | } |
| 226 | |
| 227 | return ScopedAStatus::ok(); |
Yifei Zhang | 51eba2e | 2024-10-24 15:11:54 -0700 | [diff] [blame] | 228 | }; |
| 229 | |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 230 | ScopedAStatus ContextHub::registerEndpoint(const EndpointInfo& in_endpoint) { |
| 231 | std::unique_lock<std::mutex> lock(mEndpointMutex); |
| 232 | |
| 233 | for (const EndpointInfo& endpoint : mEndpoints) { |
| 234 | if ((endpoint.id.id == in_endpoint.id.id && endpoint.id.hubId == in_endpoint.id.hubId) || |
| 235 | endpoint.name == in_endpoint.name) { |
| 236 | return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 237 | } |
| 238 | } |
| 239 | mEndpoints.push_back(in_endpoint); |
| 240 | return ScopedAStatus::ok(); |
Yifei Zhang | 51eba2e | 2024-10-24 15:11:54 -0700 | [diff] [blame] | 241 | }; |
| 242 | |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 243 | ScopedAStatus ContextHub::unregisterEndpoint(const EndpointInfo& in_endpoint) { |
| 244 | std::unique_lock<std::mutex> lock(mEndpointMutex); |
| 245 | |
| 246 | for (auto it = mEndpoints.begin(); it != mEndpoints.end(); ++it) { |
| 247 | if (it->id.id == in_endpoint.id.id && it->id.hubId == in_endpoint.id.hubId) { |
| 248 | mEndpoints.erase(it); |
| 249 | return ScopedAStatus::ok(); |
| 250 | } |
| 251 | } |
| 252 | return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
Yifei Zhang | 51eba2e | 2024-10-24 15:11:54 -0700 | [diff] [blame] | 253 | }; |
| 254 | |
| 255 | ScopedAStatus ContextHub::registerEndpointCallback( |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 256 | const std::shared_ptr<IEndpointCallback>& in_callback) { |
| 257 | std::unique_lock<std::mutex> lock(mEndpointMutex); |
| 258 | |
| 259 | mEndpointCallback = in_callback; |
| 260 | return ScopedAStatus::ok(); |
Yifei Zhang | 51eba2e | 2024-10-24 15:11:54 -0700 | [diff] [blame] | 261 | }; |
| 262 | |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 263 | ScopedAStatus ContextHub::requestSessionIdRange(int32_t in_size, |
Yifei Zhang | cef8f66 | 2024-12-06 16:03:26 -0800 | [diff] [blame] | 264 | std::array<int32_t, 2>* _aidl_return) { |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 265 | constexpr int32_t kMaxSize = 1024; |
| 266 | if (in_size > kMaxSize || _aidl_return == nullptr) { |
| 267 | return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 268 | } |
| 269 | |
| 270 | { |
| 271 | std::lock_guard<std::mutex> lock(mEndpointMutex); |
| 272 | mMaxValidSessionId = in_size; |
| 273 | } |
| 274 | |
Matthew Sedam | 0ec290c | 2024-12-11 11:18:15 -0800 | [diff] [blame] | 275 | (*_aidl_return)[0] = 0; |
| 276 | (*_aidl_return)[1] = in_size; |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 277 | return ScopedAStatus::ok(); |
Yifei Zhang | 51eba2e | 2024-10-24 15:11:54 -0700 | [diff] [blame] | 278 | }; |
| 279 | |
| 280 | ScopedAStatus ContextHub::openEndpointSession( |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 281 | int32_t in_sessionId, const EndpointId& in_destination, const EndpointId& in_initiator, |
| 282 | const std::optional<std::string>& in_serviceDescriptor) { |
| 283 | // We are not calling onCloseEndpointSession on failure because the remote endpoints (our |
| 284 | // mock endpoints) always accept the session. |
| 285 | |
Matthew Sedam | 0ec290c | 2024-12-11 11:18:15 -0800 | [diff] [blame] | 286 | std::weak_ptr<IEndpointCallback> callback; |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 287 | { |
| 288 | std::unique_lock<std::mutex> lock(mEndpointMutex); |
| 289 | if (in_sessionId > mMaxValidSessionId) { |
| 290 | ALOGE("openEndpointSession: session ID %" PRId32 " is invalid", in_sessionId); |
| 291 | return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 292 | } |
| 293 | |
| 294 | for (const EndpointSession& session : mEndpointSessions) { |
| 295 | bool sessionAlreadyExists = |
| 296 | (session.initiator == in_destination && session.peer == in_initiator) || |
| 297 | (session.peer == in_destination && session.initiator == in_initiator); |
| 298 | if (sessionAlreadyExists) { |
| 299 | ALOGD("openEndpointSession: session ID %" PRId32 " already exists", in_sessionId); |
| 300 | return (session.sessionId == in_sessionId && |
| 301 | session.serviceDescriptor == in_serviceDescriptor) |
| 302 | ? ScopedAStatus::ok() |
| 303 | : ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 304 | } else if (session.sessionId == in_sessionId) { |
| 305 | ALOGE("openEndpointSession: session ID %" PRId32 " is invalid: endpoint mismatch", |
| 306 | in_sessionId); |
| 307 | return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | // Verify the initiator and destination are valid endpoints |
| 312 | bool initiatorIsValid = findEndpoint(in_initiator, mEndpoints.begin(), mEndpoints.end()); |
| 313 | if (!initiatorIsValid) { |
| 314 | ALOGE("openEndpointSession: initiator %" PRIu64 ":%" PRIu64 " is invalid", |
| 315 | in_initiator.id, in_initiator.hubId); |
| 316 | return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 317 | } |
| 318 | bool destinationIsValid = findEndpoint(in_destination, &kMockEndpointInfos[0], |
| 319 | &kMockEndpointInfos[kMockEndpointCount]); |
| 320 | if (!destinationIsValid) { |
| 321 | ALOGE("openEndpointSession: destination %" PRIu64 ":%" PRIu64 " is invalid", |
| 322 | in_destination.id, in_destination.hubId); |
| 323 | return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 324 | } |
| 325 | |
| 326 | mEndpointSessions.push_back({ |
| 327 | .sessionId = in_sessionId, |
| 328 | .initiator = in_initiator, |
| 329 | .peer = in_destination, |
| 330 | .serviceDescriptor = in_serviceDescriptor, |
| 331 | }); |
| 332 | |
Matthew Sedam | 0ec290c | 2024-12-11 11:18:15 -0800 | [diff] [blame] | 333 | if (mEndpointCallback == nullptr) { |
| 334 | return ScopedAStatus::ok(); |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 335 | } |
Matthew Sedam | 0ec290c | 2024-12-11 11:18:15 -0800 | [diff] [blame] | 336 | callback = mEndpointCallback; |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Matthew Sedam | 0ec290c | 2024-12-11 11:18:15 -0800 | [diff] [blame] | 339 | std::unique_lock<std::mutex> lock(gCallbackMutex); |
| 340 | std::thread{[callback, in_sessionId]() { |
| 341 | std::unique_lock<std::mutex> lock(gCallbackMutex); |
| 342 | if (auto cb = callback.lock(); cb != nullptr) { |
| 343 | cb->onEndpointSessionOpenComplete(in_sessionId); |
| 344 | } |
| 345 | }}.detach(); |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 346 | return ScopedAStatus::ok(); |
Yifei Zhang | 51eba2e | 2024-10-24 15:11:54 -0700 | [diff] [blame] | 347 | }; |
| 348 | |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 349 | ScopedAStatus ContextHub::sendMessageToEndpoint(int32_t in_sessionId, const Message& in_msg) { |
Matthew Sedam | 0ec290c | 2024-12-11 11:18:15 -0800 | [diff] [blame] | 350 | std::weak_ptr<IEndpointCallback> callback; |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 351 | { |
| 352 | std::unique_lock<std::mutex> lock(mEndpointMutex); |
Matthew Sedam | 0ec290c | 2024-12-11 11:18:15 -0800 | [diff] [blame] | 353 | bool foundSession = false; |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 354 | for (const EndpointSession& session : mEndpointSessions) { |
| 355 | if (session.sessionId == in_sessionId) { |
| 356 | foundSession = true; |
| 357 | break; |
| 358 | } |
| 359 | } |
| 360 | |
Matthew Sedam | 0ec290c | 2024-12-11 11:18:15 -0800 | [diff] [blame] | 361 | if (!foundSession) { |
| 362 | ALOGE("sendMessageToEndpoint: session ID %" PRId32 " is invalid", in_sessionId); |
| 363 | return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Matthew Sedam | 0ec290c | 2024-12-11 11:18:15 -0800 | [diff] [blame] | 366 | if (mEndpointCallback == nullptr) { |
| 367 | return ScopedAStatus::ok(); |
| 368 | } |
| 369 | callback = mEndpointCallback; |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 370 | } |
Matthew Sedam | 0ec290c | 2024-12-11 11:18:15 -0800 | [diff] [blame] | 371 | |
| 372 | std::unique_lock<std::mutex> lock(gCallbackMutex); |
| 373 | if ((in_msg.flags & Message::FLAG_REQUIRES_DELIVERY_STATUS) != 0) { |
| 374 | MessageDeliveryStatus msgStatus = {}; |
| 375 | msgStatus.messageSequenceNumber = in_msg.sequenceNumber; |
| 376 | msgStatus.errorCode = ErrorCode::OK; |
| 377 | |
| 378 | std::thread{[callback, in_sessionId, msgStatus]() { |
| 379 | std::unique_lock<std::mutex> lock(gCallbackMutex); |
| 380 | if (auto cb = callback.lock(); cb != nullptr) { |
| 381 | cb->onMessageDeliveryStatusReceived(in_sessionId, msgStatus); |
| 382 | } |
| 383 | }}.detach(); |
| 384 | } |
| 385 | |
| 386 | // Echo the message back |
| 387 | std::thread{[callback, in_sessionId, in_msg]() { |
| 388 | std::unique_lock<std::mutex> lock(gCallbackMutex); |
| 389 | if (auto cb = callback.lock(); cb != nullptr) { |
| 390 | cb->onMessageReceived(in_sessionId, in_msg); |
| 391 | } |
| 392 | }}.detach(); |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 393 | return ScopedAStatus::ok(); |
Yifei Zhang | 51eba2e | 2024-10-24 15:11:54 -0700 | [diff] [blame] | 394 | }; |
| 395 | |
| 396 | ScopedAStatus ContextHub::sendMessageDeliveryStatusToEndpoint( |
| 397 | int32_t /* in_sessionId */, const MessageDeliveryStatus& /* in_msgStatus */) { |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 398 | return ScopedAStatus::ok(); |
Yifei Zhang | 51eba2e | 2024-10-24 15:11:54 -0700 | [diff] [blame] | 399 | }; |
| 400 | |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 401 | ScopedAStatus ContextHub::closeEndpointSession(int32_t in_sessionId, Reason /* in_reason */) { |
| 402 | std::unique_lock<std::mutex> lock(mEndpointMutex); |
| 403 | |
| 404 | for (auto it = mEndpointSessions.begin(); it != mEndpointSessions.end(); ++it) { |
| 405 | if (it->sessionId == in_sessionId) { |
| 406 | mEndpointSessions.erase(it); |
| 407 | return ScopedAStatus::ok(); |
| 408 | } |
| 409 | } |
| 410 | ALOGE("closeEndpointSession: session ID %" PRId32 " is invalid", in_sessionId); |
| 411 | return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
Yifei Zhang | 51eba2e | 2024-10-24 15:11:54 -0700 | [diff] [blame] | 412 | }; |
| 413 | |
| 414 | ScopedAStatus ContextHub::endpointSessionOpenComplete(int32_t /* in_sessionId */) { |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 415 | return ScopedAStatus::ok(); |
Yifei Zhang | 51eba2e | 2024-10-24 15:11:54 -0700 | [diff] [blame] | 416 | }; |
| 417 | |
Matthew Sedam | adfd557 | 2023-11-21 05:53:00 -0800 | [diff] [blame] | 418 | } // namespace aidl::android::hardware::contexthub |