• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #pragma once
15 
16 #include <cstdint>
17 
18 #include "pw_rpc/internal/packet.h"
19 #include "pw_rpc/method_id.h"
20 #include "pw_rpc/service_id.h"
21 #include "pw_span/span.h"
22 #include "pw_status/status_with_size.h"
23 
24 namespace pw::rpc {
25 
26 // Metadata about a `pw_rpc` packet.
27 //
28 // For now, this metadata structure only includes a limited set of information
29 // about the contents of a packet, but it may be extended in the future.
30 class PacketMeta {
31  public:
32   // Parses the metadata from a serialized packet.
33   static Result<PacketMeta> FromBuffer(ConstByteSpan data);
channel_id()34   constexpr uint32_t channel_id() const { return channel_id_; }
service_id()35   constexpr ServiceId service_id() const { return service_id_; }
method_id()36   constexpr MethodId method_id() const { return method_id_; }
destination_is_client()37   constexpr bool destination_is_client() const {
38     return destination_ == internal::Packet::kClient;
39   }
destination_is_server()40   constexpr bool destination_is_server() const {
41     return destination_ == internal::Packet::kServer;
42   }
43   // Note: this `payload` is only valid so long as the original `data` buffer
44   // passed to `PacketMeta::FromBuffer` remains valid.
payload()45   constexpr ConstByteSpan payload() const { return payload_; }
46 
47  private:
PacketMeta(const internal::Packet packet)48   constexpr explicit PacketMeta(const internal::Packet packet)
49       : channel_id_(packet.channel_id()),
50         service_id_(internal::WrapServiceId(packet.service_id())),
51         method_id_(internal::WrapMethodId(packet.method_id())),
52         destination_(packet.destination()),
53         payload_(packet.payload()) {}
54   uint32_t channel_id_;
55   ServiceId service_id_;
56   MethodId method_id_;
57   internal::Packet::Destination destination_;
58   ConstByteSpan payload_;
59 };
60 
61 }  // namespace pw::rpc
62