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 | #pragma once |
| 18 | |
| 19 | #include <aidl/android/hardware/contexthub/BnContextHub.h> |
| 20 | |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 21 | #include <mutex> |
Arthur Ishiguro | 065a9a5 | 2021-11-19 00:24:45 +0000 | [diff] [blame] | 22 | #include <unordered_set> |
Arthur Ishiguro | fd5e65c | 2022-11-08 16:49:47 +0000 | [diff] [blame] | 23 | #include <vector> |
Arthur Ishiguro | 065a9a5 | 2021-11-19 00:24:45 +0000 | [diff] [blame] | 24 | |
Arthur Ishiguro | a257b78 | 2021-08-04 10:40:29 -0700 | [diff] [blame] | 25 | namespace aidl { |
| 26 | namespace android { |
| 27 | namespace hardware { |
| 28 | namespace contexthub { |
| 29 | |
| 30 | class ContextHub : public BnContextHub { |
| 31 | ::ndk::ScopedAStatus getContextHubs(std::vector<ContextHubInfo>* out_contextHubInfos) override; |
| 32 | ::ndk::ScopedAStatus loadNanoapp(int32_t in_contextHubId, const NanoappBinary& in_appBinary, |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 33 | int32_t in_transactionId) override; |
Arthur Ishiguro | a257b78 | 2021-08-04 10:40:29 -0700 | [diff] [blame] | 34 | ::ndk::ScopedAStatus unloadNanoapp(int32_t in_contextHubId, int64_t in_appId, |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 35 | int32_t in_transactionId) override; |
Arthur Ishiguro | a257b78 | 2021-08-04 10:40:29 -0700 | [diff] [blame] | 36 | ::ndk::ScopedAStatus disableNanoapp(int32_t in_contextHubId, int64_t in_appId, |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 37 | int32_t in_transactionId) override; |
Arthur Ishiguro | a257b78 | 2021-08-04 10:40:29 -0700 | [diff] [blame] | 38 | ::ndk::ScopedAStatus enableNanoapp(int32_t in_contextHubId, int64_t in_appId, |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 39 | int32_t in_transactionId) override; |
Arthur Ishiguro | a257b78 | 2021-08-04 10:40:29 -0700 | [diff] [blame] | 40 | ::ndk::ScopedAStatus onSettingChanged(Setting in_setting, bool in_enabled) override; |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 41 | ::ndk::ScopedAStatus queryNanoapps(int32_t in_contextHubId) override; |
Arthur Ishiguro | fd5e65c | 2022-11-08 16:49:47 +0000 | [diff] [blame] | 42 | ::ndk::ScopedAStatus getPreloadedNanoappIds( |
Matthew Sedam | d70f84d | 2023-03-06 18:34:24 +0000 | [diff] [blame] | 43 | int32_t in_contextHubId, std::vector<int64_t>* out_preloadedNanoappIds) override; |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 44 | ::ndk::ScopedAStatus registerCallback( |
| 45 | int32_t in_contextHubId, const std::shared_ptr<IContextHubCallback>& in_cb) override; |
Arthur Ishiguro | a257b78 | 2021-08-04 10:40:29 -0700 | [diff] [blame] | 46 | ::ndk::ScopedAStatus sendMessageToHub(int32_t in_contextHubId, |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 47 | const ContextHubMessage& in_message) override; |
Matthew Sedam | c8ce4d5 | 2023-01-09 20:18:21 +0000 | [diff] [blame] | 48 | ::ndk::ScopedAStatus setTestMode(bool enable) override; |
Arthur Ishiguro | 065a9a5 | 2021-11-19 00:24:45 +0000 | [diff] [blame] | 49 | ::ndk::ScopedAStatus onHostEndpointConnected(const HostEndpointInfo& in_info) override; |
| 50 | |
| 51 | ::ndk::ScopedAStatus onHostEndpointDisconnected(char16_t in_hostEndpointId) override; |
Anthony Stange | 7fba100 | 2023-03-02 21:45:20 +0000 | [diff] [blame] | 52 | ::ndk::ScopedAStatus onNanSessionStateChanged(const NanSessionStateUpdate& in_update) override; |
Matthew Sedam | adfd557 | 2023-11-21 05:53:00 -0800 | [diff] [blame] | 53 | ::ndk::ScopedAStatus sendMessageDeliveryStatusToHub( |
| 54 | int32_t in_contextHubId, |
| 55 | const MessageDeliveryStatus& in_messageDeliveryStatus) override; |
Arthur Ishiguro | 94e1aa2 | 2021-10-26 17:25:19 +0000 | [diff] [blame] | 56 | |
Yifei Zhang | 51eba2e | 2024-10-24 15:11:54 -0700 | [diff] [blame] | 57 | ::ndk::ScopedAStatus getHubs(std::vector<HubInfo>* _aidl_return) override; |
| 58 | ::ndk::ScopedAStatus getEndpoints(std::vector<EndpointInfo>* _aidl_return) override; |
| 59 | ::ndk::ScopedAStatus registerEndpoint(const EndpointInfo& in_endpoint) override; |
| 60 | ::ndk::ScopedAStatus unregisterEndpoint(const EndpointInfo& in_endpoint) override; |
| 61 | ::ndk::ScopedAStatus registerEndpointCallback( |
| 62 | const std::shared_ptr<IEndpointCallback>& in_callback) override; |
| 63 | ::ndk::ScopedAStatus requestSessionIdRange(int32_t in_size, |
| 64 | std::vector<int32_t>* _aidl_return) override; |
| 65 | ::ndk::ScopedAStatus openEndpointSession( |
| 66 | int32_t in_sessionId, const EndpointId& in_destination, const EndpointId& in_initiator, |
| 67 | const std::optional<std::string>& in_serviceDescriptor) override; |
| 68 | ::ndk::ScopedAStatus sendMessageToEndpoint(int32_t in_sessionId, |
| 69 | const Message& in_msg) override; |
| 70 | ::ndk::ScopedAStatus sendMessageDeliveryStatusToEndpoint( |
| 71 | int32_t in_sessionId, const MessageDeliveryStatus& in_msgStatus) override; |
| 72 | ::ndk::ScopedAStatus closeEndpointSession(int32_t in_sessionId, Reason in_reason) override; |
| 73 | ::ndk::ScopedAStatus endpointSessionOpenComplete(int32_t in_sessionId) override; |
| 74 | |
Arthur Ishiguro | 94e1aa2 | 2021-10-26 17:25:19 +0000 | [diff] [blame] | 75 | private: |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 76 | struct EndpointSession { |
| 77 | int32_t sessionId; |
| 78 | EndpointId initiator; |
| 79 | EndpointId peer; |
| 80 | std::optional<std::string> serviceDescriptor; |
| 81 | }; |
| 82 | |
Arthur Ishiguro | 94e1aa2 | 2021-10-26 17:25:19 +0000 | [diff] [blame] | 83 | static constexpr uint32_t kMockHubId = 0; |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 84 | |
| 85 | //! Finds an endpoint in the range defined by the endpoints |
| 86 | //! @return whether the endpoint was found |
| 87 | template <typename Iter> |
| 88 | bool findEndpoint(const EndpointId& target, const Iter& begin, const Iter& end) { |
| 89 | for (auto iter = begin; iter != end; ++iter) { |
| 90 | if (iter->id.id == target.id && iter->id.hubId == target.hubId) { |
| 91 | return true; |
| 92 | } |
| 93 | } |
| 94 | return false; |
| 95 | } |
| 96 | |
Arthur Ishiguro | 94e1aa2 | 2021-10-26 17:25:19 +0000 | [diff] [blame] | 97 | std::shared_ptr<IContextHubCallback> mCallback; |
Arthur Ishiguro | 065a9a5 | 2021-11-19 00:24:45 +0000 | [diff] [blame] | 98 | |
| 99 | std::unordered_set<char16_t> mConnectedHostEndpoints; |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 100 | |
| 101 | //! Endpoint storage and information |
| 102 | std::mutex mEndpointMutex; |
| 103 | std::vector<EndpointInfo> mEndpoints; |
| 104 | std::vector<EndpointSession> mEndpointSessions; |
| 105 | std::shared_ptr<IEndpointCallback> mEndpointCallback; |
| 106 | int32_t mMaxValidSessionId = 0; |
Arthur Ishiguro | a257b78 | 2021-08-04 10:40:29 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | } // namespace contexthub |
| 110 | } // namespace hardware |
| 111 | } // namespace android |
| 112 | } // namespace aidl |