xref: /aosp_15_r20/system/chre/host/hal_generic/common/context_hub_v4_impl.h (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2024 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 <functional>
20 #include <optional>
21 #include <string>
22 #include <vector>
23 
24 #include <aidl/android/hardware/contexthub/BnContextHub.h>
25 #include <chre_host/generated/host_messages_generated.h>
26 
27 #include "message_hub_manager.h"
28 
29 namespace android::hardware::contexthub::common::implementation {
30 
31 using ::aidl::android::hardware::contexthub::EndpointId;
32 using ::aidl::android::hardware::contexthub::EndpointInfo;
33 using ::aidl::android::hardware::contexthub::HubInfo;
34 using ::aidl::android::hardware::contexthub::IEndpointCallback;
35 using ::aidl::android::hardware::contexthub::Message;
36 using ::aidl::android::hardware::contexthub::MessageDeliveryStatus;
37 using ::aidl::android::hardware::contexthub::Reason;
38 using ::ndk::ScopedAStatus;
39 
40 class ContextHubV4Impl {
41  public:
42   using SendMessageFn = std::function<bool(uint8_t *data, size_t size)>;
43 
ContextHubV4Impl(SendMessageFn sendMessageFn)44   explicit ContextHubV4Impl(SendMessageFn sendMessageFn)
45       : mManager([this](int64_t id) { onHostHubDown(id); }),
46         mSendMessageFn(std::move(sendMessageFn)) {}
47   ~ContextHubV4Impl() = default;
48 
49   /**
50    * Initializes the implementation.
51    *
52    * This should be called once a connection with CHRE has been established.
53    * Requests a dump of embedded hubs and endpoints from CHRE.
54    */
55   void init();
56 
57   // ContextHub V4 API implementation.
58   ScopedAStatus getHubs(std::vector<HubInfo> *hubs);
59   ScopedAStatus getEndpoints(std::vector<EndpointInfo> *endpoints);
60   ScopedAStatus registerEndpoint(const EndpointInfo &endpoint);
61   ScopedAStatus unregisterEndpoint(const EndpointInfo &endpoint);
62   ScopedAStatus registerEndpointCallback(
63       const std::shared_ptr<IEndpointCallback> &callback);
64   ScopedAStatus requestSessionIdRange(int32_t size, std::vector<int32_t> *ids);
65   ScopedAStatus openEndpointSession(
66       int32_t sessionId, const EndpointId &destination,
67       const EndpointId &initiator,
68       const std::optional<std::string> &serviceDescriptor);
69   ScopedAStatus sendMessageToEndpoint(int32_t sessionId, const Message &msg);
70   ScopedAStatus sendMessageDeliveryStatusToEndpoint(
71       int32_t sessionId, const MessageDeliveryStatus &msgStatus);
72   ScopedAStatus closeEndpointSession(int32_t sessionId, Reason reason);
73   ScopedAStatus endpointSessionOpenComplete(int32_t sessionId);
74 
75   /**
76    * Handles a CHRE message that is part of the V4 implementation.
77    *
78    * @param message Validated union of the various message types.
79    * @return true if the message could be handled
80    */
81   bool handleMessageFromChre(const ::chre::fbs::ChreMessageUnion &message);
82 
83  private:
84   // Callbacks for each message type from CHRE.
85   void onGetMessageHubsAndEndpointsResponse(
86       const ::chre::fbs::GetMessageHubsAndEndpointsResponseT &msg);
87   void onRegisterMessageHub(const ::chre::fbs::RegisterMessageHubT &msg);
88   void onUnregisterMessageHub(const ::chre::fbs::UnregisterMessageHubT &msg);
89   void onRegisterEndpoint(const ::chre::fbs::RegisterEndpointT &msg);
90   void onUnregisterEndpoint(const ::chre::fbs::UnregisterEndpointT &msg);
91   void onOpenEndpointSessionRequest(
92       const ::chre::fbs::OpenEndpointSessionRequestT &msg);
93   void onEndpointSessionOpened(const ::chre::fbs::EndpointSessionOpenedT &msg);
94   void onEndpointSessionClosed(const ::chre::fbs::EndpointSessionClosedT &msg);
95   void onEndpointSessionMessage(
96       const ::chre::fbs::EndpointSessionMessageT &msg);
97   void onEndpointSessionMessageDeliveryStatus(
98       const ::chre::fbs::EndpointSessionMessageDeliveryStatusT &msg);
99 
100   // Callback invoked when a HAL client associated with a host hub goes down.
101   void onHostHubDown(int64_t id);
102 
103   MessageHubManager mManager;
104   SendMessageFn mSendMessageFn;
105 };
106 
107 }  // namespace android::hardware::contexthub::common::implementation
108