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_UTIL_SYSTEM_MESSAGE_COMMON_H_ 18 #define CHRE_UTIL_SYSTEM_MESSAGE_COMMON_H_ 19 20 #include <pw_allocator/unique_ptr.h> 21 #include <pw_function/function.h> 22 #include <cstddef> 23 #include <cstdint> 24 #include <cstring> 25 26 namespace chre::message { 27 28 //! The ID of a MessageHub 29 using MessageHubId = uint64_t; 30 31 //! The ID of an endpoint 32 using EndpointId = uint64_t; 33 34 //! The ID of a session 35 using SessionId = uint16_t; 36 37 //! An invalid MessageHub ID 38 constexpr MessageHubId MESSAGE_HUB_ID_INVALID = 0; 39 40 //! An invalid endpoint ID 41 constexpr EndpointId ENDPOINT_ID_INVALID = 0; 42 43 //! An invalid session ID 44 constexpr SessionId SESSION_ID_INVALID = UINT16_MAX; 45 46 //! Endpoint types 47 enum class EndpointType : uint8_t { 48 HOST_FRAMEWORK = 1, 49 HOST_APP = 2, 50 HOST_NATIVE = 3, 51 NANOAPP = 4, 52 GENERIC = 5, 53 }; 54 55 //! Endpoint permissions 56 //! This should match CHRE permissions. 57 // TODO(b/373417024): Update permissions to this typed name in all MessageRouter 58 // code 59 enum class EndpointPermission : uint32_t { 60 NONE = 0, 61 AUDIO = 1, 62 GNSS = 1 << 1, 63 WIFI = 1 << 2, 64 WWAN = 1 << 3, 65 BLE = 1 << 4, 66 }; 67 68 //! Represents a single endpoint connected to a MessageHub 69 struct Endpoint { 70 MessageHubId messageHubId; 71 EndpointId endpointId; 72 73 bool operator==(const Endpoint &other) const { 74 return messageHubId == other.messageHubId && endpointId == other.endpointId; 75 } 76 77 bool operator!=(const Endpoint &other) const { 78 return !(*this == other); 79 } 80 }; 81 82 //! Represents a session between two endpoints 83 struct Session { 84 SessionId sessionId; 85 Endpoint initiator; 86 Endpoint peer; 87 88 bool operator==(const Session &other) const { 89 return sessionId == other.sessionId && initiator == other.initiator && 90 peer == other.peer; 91 } 92 93 bool operator!=(const Session &other) const { 94 return !(*this == other); 95 } 96 isEquivalentSession97 bool isEquivalent(const Session &other) const { 98 return (initiator == other.initiator && peer == other.peer) || 99 (initiator == other.peer && peer == other.initiator); 100 } 101 }; 102 103 //! Represents a message sent using the MessageRouter 104 struct Message { 105 Endpoint sender; 106 Endpoint recipient; 107 SessionId sessionId; 108 pw::UniquePtr<std::byte[]> data; 109 size_t length; 110 uint32_t messageType; 111 uint32_t messagePermissions; 112 MessageMessage113 Message() 114 : sessionId(SESSION_ID_INVALID), 115 data(nullptr), 116 length(0), 117 messageType(0), 118 messagePermissions(0) {} MessageMessage119 Message(pw::UniquePtr<std::byte[]> &&data, size_t length, 120 uint32_t messageType, uint32_t messagePermissions, Session session, 121 bool sentBySessionInitiator) 122 : sender(sentBySessionInitiator ? session.initiator : session.peer), 123 recipient(sentBySessionInitiator ? session.peer : session.initiator), 124 sessionId(session.sessionId), 125 data(std::move(data)), 126 length(length), 127 messageType(messageType), 128 messagePermissions(messagePermissions) {} MessageMessage129 Message(Message &&other) 130 : sender(other.sender), 131 recipient(other.recipient), 132 sessionId(other.sessionId), 133 data(std::move(other.data)), 134 length(other.length), 135 messageType(other.messageType), 136 messagePermissions(other.messagePermissions) {} 137 138 Message(const Message &) = delete; 139 Message &operator=(const Message &) = delete; 140 141 Message &operator=(Message &&other) { 142 sender = other.sender; 143 recipient = other.recipient; 144 sessionId = other.sessionId; 145 data = std::move(other.data); 146 length = other.length; 147 messageType = other.messageType; 148 messagePermissions = other.messagePermissions; 149 return *this; 150 } 151 }; 152 153 //! Represents information about an endpoint 154 //! Service information is stored in ServiceManager 155 struct EndpointInfo { 156 static constexpr size_t kMaxNameLength = 50; 157 EndpointInfoEndpointInfo158 EndpointInfo(EndpointId id, const char *name, uint32_t version, 159 EndpointType type, uint32_t requiredPermissions) 160 : id(id), 161 version(version), 162 type(type), 163 requiredPermissions(requiredPermissions) { 164 if (name != nullptr) { 165 std::strncpy(this->name, name, kMaxNameLength); 166 } else { 167 this->name[0] = '\0'; 168 } 169 this->name[kMaxNameLength] = '\0'; 170 } 171 172 EndpointId id; 173 char name[kMaxNameLength + 1]; 174 uint32_t version; 175 EndpointType type; 176 uint32_t requiredPermissions; 177 178 bool operator==(const EndpointInfo &other) const { 179 return id == other.id && version == other.version && type == other.type && 180 requiredPermissions == other.requiredPermissions && 181 std::strcmp(name, other.name) == 0; 182 } 183 184 bool operator!=(const EndpointInfo &other) const { 185 return !(*this == other); 186 } 187 }; 188 189 //! Represents information about a MessageHub 190 struct MessageHubInfo { 191 MessageHubId id; 192 const char *name; 193 194 bool operator==(const MessageHubInfo &other) const { 195 return id == other.id && std::strcmp(name, other.name) == 0; 196 } 197 198 bool operator!=(const MessageHubInfo &other) const { 199 return !(*this == other); 200 } 201 }; 202 203 } // namespace chre::message 204 205 #endif // CHRE_UTIL_SYSTEM_MESSAGE_COMMON_H_ 206