Line data Source code
1 : /********************************************************************************
2 : * Copyright (c) 2026 Accenture
3 : *
4 : * This program and the accompanying materials are made available under the
5 : * terms of the Apache License Version 2.0 which is available at
6 : * https://www.apache.org/licenses/LICENSE-2.0
7 : *
8 : * SPDX-License-Identifier: Apache-2.0
9 : ********************************************************************************/
10 :
11 : #pragma once
12 :
13 : #include "someip/SomeIpConstants.h"
14 :
15 : #include <etl/span.h>
16 :
17 : #include <cstdint>
18 :
19 : namespace someip
20 : {
21 : /*
22 : * \return Length of a message, without header.
23 : */
24 : uint32_t readLength(::etl::span<uint8_t const> const& messageBuffer);
25 :
26 : /**
27 : * \return Total length of message including header.
28 : */
29 : uint32_t readTotalLength(::etl::span<uint8_t const> const& messageBuffer);
30 :
31 : /**
32 : * Class representing a SOME/IP Message and providing methods for its
33 : * serialization.
34 : *
35 : * \section header SomeIp Header
36 : * \li 32 bit: Message ID (Service ID / Method ID)
37 : * \li 32 bit: Length
38 : * \li 32 bit: Request ID (Client ID / Session ID)
39 : * \li 8 bit: Protocol Version
40 : * \li 8 bit: Interface Version
41 : * \li 8 bit: Message Type
42 : * \li 8 bit: Return Code
43 : */
44 : class SomeIpMessage
45 : {
46 : public:
47 : static uint32_t const OFFSET_MESSAGE_ID = 0U;
48 : static uint32_t const OFFSET_SERVICE_ID = 0U;
49 : static uint32_t const OFFSET_METHOD_ID = 2U;
50 : static uint32_t const OFFSET_LENGTH = 4U;
51 : static uint32_t const OFFSET_REQUEST_ID = 8U;
52 : static uint32_t const OFFSET_CLIENT_ID = 8U;
53 : static uint32_t const OFFSET_SESSION_ID = 10U;
54 : static uint32_t const OFFSET_PROTOCOL_VERSION = 12U;
55 : static uint32_t const OFFSET_INTERFACE_VERSION = 13U;
56 : static uint32_t const OFFSET_MESSAGE_TYPE = 14U;
57 : static uint32_t const OFFSET_RETURN_CODE = 15U;
58 : static uint32_t const OFFSET_PAYLOAD = 16U;
59 : static uint32_t const OFFSET_SD_FLAGS = 0U;
60 :
61 : /** Possible types of a SOME/IP message */
62 : enum class MessageType : uint8_t
63 : {
64 : REQUEST = 0x00,
65 : REQUEST_NO_RETURN = 0x01,
66 : NOTIFICATION = 0x02,
67 : RESPONSE = 0x80,
68 : EXCEPTION = 0x81
69 : };
70 :
71 : enum class ReturnCode : uint8_t
72 : {
73 : SOMEIP_E_OK = 0x00,
74 : SOMEIP_E_NOT_OK = 0x01,
75 : SOMEIP_E_UNKNOWN_SERVICE = 0x02,
76 : SOMEIP_E_UNKNOWN_METHOD = 0x03,
77 : SOMEIP_E_NOT_READY = 0x04,
78 : SOMEIP_E_NOT_REACHABLE = 0x05,
79 : SOMEIP_E_TIMEOUT = 0x06,
80 : SOMEIP_E_WRONG_PROTOCOL_VERSION = 0x07,
81 : SOMEIP_E_WRONG_INTERFACE_VERSION = 0x08,
82 : SOMEIP_E_MALFORMED_MESSAGE = 0x09,
83 : SOMEIP_E_WRONG_MESSAGE_TYPE = 0x0A
84 : };
85 :
86 : /**
87 : * Create a SOME/IP message and use the specified buffer as the data
88 : * for the message.
89 : *
90 : * \param buffer The buffer where the message will be read or written.
91 : */
92 247 : explicit SomeIpMessage(::etl::span<uint8_t> const& buffer) : _buffer(buffer) {}
93 :
94 : SomeIpMessage(SomeIpMessage const&) = delete;
95 : SomeIpMessage& operator=(SomeIpMessage const&) = delete;
96 :
97 : /**
98 : * Returns the message id, which is composed of service id and method
99 : * id where 2 bytes are used as service id followed by 2 bytes for method id.
100 : */
101 : uint32_t getMessageId() const;
102 :
103 : /** \see getMessageId() */
104 : void setMessageId(uint32_t messageId);
105 :
106 : /** Returns the service id for this message */
107 : service_id::type getServiceId() const;
108 :
109 : /** Sets the service id to the specified id */
110 : void setServiceId(service_id::type serviceId);
111 :
112 : /** Returns the method id for this message */
113 : uint16_t getMethodId() const;
114 :
115 : /** Sets the method id to the specified id */
116 : void setMethodId(uint16_t methodId);
117 :
118 : /** Returns the length of message without header */
119 107 : uint32_t getLength() const { return readLength(_buffer); }
120 :
121 : /** Sets the length of this message */
122 : void setLength(uint32_t length);
123 :
124 : /**
125 : * aka SessionId
126 : */
127 : uint32_t getRequestId() const;
128 : void setRequestId(uint32_t requestId);
129 :
130 : uint16_t getClientId() const;
131 : void setClientId(uint16_t clientId);
132 :
133 : uint16_t getSessionId() const;
134 : void setSessionId(uint16_t sessionId);
135 :
136 : uint8_t getProtocolVersion() const;
137 : void setProtocolVersion(uint8_t protocolVersion);
138 :
139 : uint8_t getInterfaceVersion() const;
140 : void setInterfaceVersion(uint8_t interfaceVersion);
141 :
142 : MessageType getMessageType() const;
143 : void setMessageType(MessageType messageType);
144 :
145 : void setRawMessageType(uint8_t messageType);
146 : uint8_t getRawMessageType() const;
147 :
148 : ReturnCode getReturnCode() const;
149 : void setReturnCode(ReturnCode returnCode);
150 :
151 : ::etl::span<uint8_t const> getBufferHeader() const;
152 : ::etl::span<uint8_t const> getBufferPayload() const;
153 :
154 : uint8_t const* getPayload() const;
155 :
156 : /** Returns a pointer to the message beginning after the header */
157 : uint8_t* getPayload();
158 :
159 : /** Returns a pointer to the raw buffer containing the entire message */
160 1 : uint8_t* getRawData() const { return _buffer.data(); }
161 :
162 : /**
163 : * \return Length of payload.
164 : */
165 : uint32_t getPayloadLength() const;
166 :
167 : void setPayloadLength(uint32_t payloadLength);
168 :
169 : /**
170 : * \return Total length of message including header.
171 : */
172 60 : uint32_t getTotalLength() const { return readTotalLength(_buffer); }
173 :
174 : /**
175 : * \return Maximum length of payload.
176 : */
177 : uint32_t getMaximumPayloadLength() const;
178 :
179 : uint8_t getFlags() const;
180 :
181 : /**
182 : * \return Client to server magic cookie message.
183 : */
184 : static void makeClientToServerMagicCookieMessage(SomeIpMessage& message);
185 :
186 : /**
187 : * \return Server to client magic cookie message.
188 : */
189 : static void makeServerToClientMagicCookieMessage(SomeIpMessage& message);
190 :
191 : private:
192 : static uint32_t const INVALID_VALUE = 0xFFFFFFFFU;
193 :
194 : ::etl::span<uint8_t> _buffer;
195 : };
196 : } // namespace someip
|