C/C++ API Reference
Loading...
Searching...
No Matches
packet_meta.h
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/internal/packet.pwpb.h"
20#include "pw_rpc/method_id.h"
21#include "pw_rpc/service_id.h"
22#include "pw_span/span.h"
23#include "pw_status/status_with_size.h"
24
25namespace pw::rpc {
26
28
29// Metadata about a `pw_rpc` packet.
30//
31// For now, this metadata structure only includes a limited set of information
32// about the contents of a packet, but it may be extended in the future.
34 public:
35 // Parses the metadata from a serialized packet.
36 static Result<PacketMeta> FromBuffer(ConstByteSpan data);
37 constexpr uint32_t channel_id() const { return channel_id_; }
38 constexpr ServiceId service_id() const { return service_id_; }
39 constexpr MethodId method_id() const { return method_id_; }
40 constexpr bool destination_is_client() const {
41 return destination_ == internal::Packet::kClient;
42 }
43 constexpr bool destination_is_server() const {
44 return destination_ == internal::Packet::kServer;
45 }
46 constexpr bool type_is_client_error() const {
47 return type_ == internal::pwpb::PacketType::CLIENT_ERROR;
48 }
49 constexpr bool type_is_server_error() const {
50 return type_ == internal::pwpb::PacketType::SERVER_ERROR;
51 }
52 // Note: this `payload` is only valid so long as the original `data` buffer
53 // passed to `PacketMeta::FromBuffer` remains valid.
54 constexpr ConstByteSpan payload() const { return payload_; }
55
56 private:
57 constexpr explicit PacketMeta(const internal::Packet packet)
58 : channel_id_(packet.channel_id()),
59 service_id_(internal::WrapServiceId(packet.service_id())),
60 method_id_(internal::WrapMethodId(packet.method_id())),
61 destination_(packet.destination()),
62 type_(packet.type()),
63 payload_(packet.payload()) {}
64 uint32_t channel_id_;
65 ServiceId service_id_;
66 MethodId method_id_;
67 internal::Packet::Destination destination_;
68 internal::pwpb::PacketType type_;
69 ConstByteSpan payload_;
70};
71
73
74} // namespace pw::rpc
Definition: poll.h:25
Definition: method_id.h:32
Definition: packet_meta.h:33
Definition: service_id.h:35