xref: /aosp_15_r20/hardware/interfaces/contexthub/aidl/default/include/contexthub-impl/ContextHub.h (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
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 
21 #include <mutex>
22 #include <unordered_set>
23 #include <vector>
24 
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,
33                                      int32_t in_transactionId) override;
34     ::ndk::ScopedAStatus unloadNanoapp(int32_t in_contextHubId, int64_t in_appId,
35                                        int32_t in_transactionId) override;
36     ::ndk::ScopedAStatus disableNanoapp(int32_t in_contextHubId, int64_t in_appId,
37                                         int32_t in_transactionId) override;
38     ::ndk::ScopedAStatus enableNanoapp(int32_t in_contextHubId, int64_t in_appId,
39                                        int32_t in_transactionId) override;
40     ::ndk::ScopedAStatus onSettingChanged(Setting in_setting, bool in_enabled) override;
41     ::ndk::ScopedAStatus queryNanoapps(int32_t in_contextHubId) override;
42     ::ndk::ScopedAStatus getPreloadedNanoappIds(
43             int32_t in_contextHubId, std::vector<int64_t>* out_preloadedNanoappIds) override;
44     ::ndk::ScopedAStatus registerCallback(
45             int32_t in_contextHubId, const std::shared_ptr<IContextHubCallback>& in_cb) override;
46     ::ndk::ScopedAStatus sendMessageToHub(int32_t in_contextHubId,
47                                           const ContextHubMessage& in_message) override;
48     ::ndk::ScopedAStatus setTestMode(bool enable) override;
49     ::ndk::ScopedAStatus onHostEndpointConnected(const HostEndpointInfo& in_info) override;
50 
51     ::ndk::ScopedAStatus onHostEndpointDisconnected(char16_t in_hostEndpointId) override;
52     ::ndk::ScopedAStatus onNanSessionStateChanged(const NanSessionStateUpdate& in_update) override;
53     ::ndk::ScopedAStatus sendMessageDeliveryStatusToHub(
54             int32_t in_contextHubId,
55             const MessageDeliveryStatus& in_messageDeliveryStatus) override;
56 
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 
75   private:
76     struct EndpointSession {
77         int32_t sessionId;
78         EndpointId initiator;
79         EndpointId peer;
80         std::optional<std::string> serviceDescriptor;
81     };
82 
83     static constexpr uint32_t kMockHubId = 0;
84 
85     //! Finds an endpoint in the range defined by the endpoints
86     //! @return whether the endpoint was found
87     template <typename Iter>
findEndpoint(const EndpointId & target,const Iter & begin,const Iter & end)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 
97     std::shared_ptr<IContextHubCallback> mCallback;
98 
99     std::unordered_set<char16_t> mConnectedHostEndpoints;
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;
107 };
108 
109 }  // namespace contexthub
110 }  // namespace hardware
111 }  // namespace android
112 }  // namespace aidl
113