blob: 49688786c1e07023479e2e98ac7c2c8a3cab1a74 [file] [log] [blame]
Arthur Ishiguroa257b782021-08-04 10:40:29 -07001/*
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 Sedam92c2bd82024-11-11 19:52:30 +000021#include <mutex>
Arthur Ishiguro065a9a52021-11-19 00:24:45 +000022#include <unordered_set>
Arthur Ishigurofd5e65c2022-11-08 16:49:47 +000023#include <vector>
Arthur Ishiguro065a9a52021-11-19 00:24:45 +000024
Arthur Ishiguroa257b782021-08-04 10:40:29 -070025namespace aidl {
26namespace android {
27namespace hardware {
28namespace contexthub {
29
30class 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 Ishiguro070f47d2022-01-06 22:42:10 +000033 int32_t in_transactionId) override;
Arthur Ishiguroa257b782021-08-04 10:40:29 -070034 ::ndk::ScopedAStatus unloadNanoapp(int32_t in_contextHubId, int64_t in_appId,
Arthur Ishiguro070f47d2022-01-06 22:42:10 +000035 int32_t in_transactionId) override;
Arthur Ishiguroa257b782021-08-04 10:40:29 -070036 ::ndk::ScopedAStatus disableNanoapp(int32_t in_contextHubId, int64_t in_appId,
Arthur Ishiguro070f47d2022-01-06 22:42:10 +000037 int32_t in_transactionId) override;
Arthur Ishiguroa257b782021-08-04 10:40:29 -070038 ::ndk::ScopedAStatus enableNanoapp(int32_t in_contextHubId, int64_t in_appId,
Arthur Ishiguro070f47d2022-01-06 22:42:10 +000039 int32_t in_transactionId) override;
Arthur Ishiguroa257b782021-08-04 10:40:29 -070040 ::ndk::ScopedAStatus onSettingChanged(Setting in_setting, bool in_enabled) override;
Arthur Ishiguro070f47d2022-01-06 22:42:10 +000041 ::ndk::ScopedAStatus queryNanoapps(int32_t in_contextHubId) override;
Arthur Ishigurofd5e65c2022-11-08 16:49:47 +000042 ::ndk::ScopedAStatus getPreloadedNanoappIds(
Matthew Sedamd70f84d2023-03-06 18:34:24 +000043 int32_t in_contextHubId, std::vector<int64_t>* out_preloadedNanoappIds) override;
Arthur Ishiguro070f47d2022-01-06 22:42:10 +000044 ::ndk::ScopedAStatus registerCallback(
45 int32_t in_contextHubId, const std::shared_ptr<IContextHubCallback>& in_cb) override;
Arthur Ishiguroa257b782021-08-04 10:40:29 -070046 ::ndk::ScopedAStatus sendMessageToHub(int32_t in_contextHubId,
Arthur Ishiguro070f47d2022-01-06 22:42:10 +000047 const ContextHubMessage& in_message) override;
Matthew Sedamc8ce4d52023-01-09 20:18:21 +000048 ::ndk::ScopedAStatus setTestMode(bool enable) override;
Arthur Ishiguro065a9a52021-11-19 00:24:45 +000049 ::ndk::ScopedAStatus onHostEndpointConnected(const HostEndpointInfo& in_info) override;
50
51 ::ndk::ScopedAStatus onHostEndpointDisconnected(char16_t in_hostEndpointId) override;
Anthony Stange7fba1002023-03-02 21:45:20 +000052 ::ndk::ScopedAStatus onNanSessionStateChanged(const NanSessionStateUpdate& in_update) override;
Matthew Sedamadfd5572023-11-21 05:53:00 -080053 ::ndk::ScopedAStatus sendMessageDeliveryStatusToHub(
54 int32_t in_contextHubId,
55 const MessageDeliveryStatus& in_messageDeliveryStatus) override;
Arthur Ishiguro94e1aa22021-10-26 17:25:19 +000056
Yifei Zhang51eba2e2024-10-24 15:11:54 -070057 ::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 Ishiguro94e1aa22021-10-26 17:25:19 +000075 private:
Matthew Sedam92c2bd82024-11-11 19:52:30 +000076 struct EndpointSession {
77 int32_t sessionId;
78 EndpointId initiator;
79 EndpointId peer;
80 std::optional<std::string> serviceDescriptor;
81 };
82
Arthur Ishiguro94e1aa22021-10-26 17:25:19 +000083 static constexpr uint32_t kMockHubId = 0;
Matthew Sedam92c2bd82024-11-11 19:52:30 +000084
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 Ishiguro94e1aa22021-10-26 17:25:19 +000097 std::shared_ptr<IContextHubCallback> mCallback;
Arthur Ishiguro065a9a52021-11-19 00:24:45 +000098
99 std::unordered_set<char16_t> mConnectedHostEndpoints;
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000100
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 Ishiguroa257b782021-08-04 10:40:29 -0700107};
108
109} // namespace contexthub
110} // namespace hardware
111} // namespace android
112} // namespace aidl