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 #ifndef CHRE_CORE_CHRE_MESSAGE_HUB_MANAGER_H_ 18 #define CHRE_CORE_CHRE_MESSAGE_HUB_MANAGER_H_ 19 20 #ifdef CHRE_MESSAGE_ROUTER_SUPPORT_ENABLED 21 22 #include "chre/core/event_loop_common.h" 23 #include "chre/util/non_copyable.h" 24 #include "chre/util/system/message_common.h" 25 #include "chre/util/system/message_router.h" 26 #include "chre/util/unique_ptr.h" 27 28 #include <cinttypes> 29 #include <optional> 30 31 namespace chre { 32 33 //! Manager class for the CHRE Message Hub. 34 class ChreMessageHubManager 35 : public NonCopyable, 36 public message::MessageRouter::MessageHubCallback { 37 public: 38 //! The ID of the CHRE MessageHub 39 constexpr static message::MessageHubId kChreMessageHubId = CHRE_PLATFORM_ID; 40 41 //! Initializes the ChreMessageHubManager 42 void init(); 43 44 //! Returns the MessageHub for the CHRE Message Hub getMessageHub()45 message::MessageRouter::MessageHub &getMessageHub() { 46 return mChreMessageHub; 47 } 48 49 private: 50 //! Data to be passed to the message callback 51 struct MessageCallbackData { 52 chreMessageFromEndpointData messageToNanoapp; 53 pw::UniquePtr<std::byte[]> data; 54 uint64_t nanoappId; 55 }; 56 57 //! Data to be passed to the session closed callback 58 struct SessionClosedCallbackData { 59 chreEndpointSessionClosedData sessionClosedData; 60 uint64_t nanoappId; 61 }; 62 63 //! Callback to process message sent to a nanoapp - used by the event loop 64 static void onMessageToNanoappCallback( 65 SystemCallbackType /* type */, 66 UniquePtr<ChreMessageHubManager::MessageCallbackData> &&data); 67 68 //! Callback to process session closed event for a nanoapp - used by the event 69 //! loop 70 static void onSessionClosedCallback( 71 SystemCallbackType /* type */, 72 UniquePtr<ChreMessageHubManager::SessionClosedCallbackData> &&data); 73 74 //! Definitions for MessageHubCallback 75 //! @see MessageRouter::MessageHubCallback 76 bool onMessageReceived(pw::UniquePtr<std::byte[]> &&data, size_t length, 77 uint32_t messageType, uint32_t messagePermissions, 78 const message::Session &session, 79 bool sentBySessionInitiator) override; 80 void onSessionClosed(const message::Session &session) override; 81 void forEachEndpoint(const pw::Function<bool(const message::EndpointInfo &)> 82 &function) override; 83 std::optional<message::EndpointInfo> getEndpointInfo( 84 message::EndpointId endpointId) override; 85 86 message::MessageRouter::MessageHub mChreMessageHub; 87 }; 88 89 } // namespace chre 90 91 #endif // CHRE_MESSAGE_ROUTER_SUPPORT_ENABLED 92 93 #endif // CHRE_CORE_CHRE_MESSAGE_HUB_MANAGER_H_ 94