aboutsummaryrefslogtreecommitdiff
path: root/pw_rpc/packet.cc
blob: 3e9803c23146ed282c0a4e24b0dd9fd92afa58e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2020 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

#include "pw_rpc/internal/packet.h"

#include "pw_protobuf/decoder.h"

namespace pw::rpc::internal {

using pwpb::PacketType;

namespace RpcPacket = pwpb::RpcPacket;

Result<Packet> Packet::FromBuffer(ConstByteSpan data) {
  Packet packet;
  Status status;
  protobuf::Decoder decoder(data);

  while ((status = decoder.Next()).ok()) {
    RpcPacket::Fields field =
        static_cast<RpcPacket::Fields>(decoder.FieldNumber());

    switch (field) {
      case RpcPacket::Fields::kType: {
        uint32_t value;
        // A decode error will propagate from Next() and terminate the loop.
        decoder.ReadUint32(&value).IgnoreError();
        packet.set_type(static_cast<PacketType>(value));
        break;
      }

      case RpcPacket::Fields::kChannelId:
        // A decode error will propagate from Next() and terminate the loop.
        decoder.ReadUint32(&packet.channel_id_).IgnoreError();
        break;

      case RpcPacket::Fields::kServiceId:
        // A decode error will propagate from Next() and terminate the loop.
        decoder.ReadFixed32(&packet.service_id_).IgnoreError();
        break;

      case RpcPacket::Fields::kMethodId:
        // A decode error will propagate from Next() and terminate the loop.
        decoder.ReadFixed32(&packet.method_id_).IgnoreError();
        break;

      case RpcPacket::Fields::kPayload:
        // A decode error will propagate from Next() and terminate the loop.
        decoder.ReadBytes(&packet.payload_).IgnoreError();
        break;

      case RpcPacket::Fields::kStatus: {
        uint32_t value;
        // A decode error will propagate from Next() and terminate the loop.
        decoder.ReadUint32(&value).IgnoreError();
        packet.set_status(static_cast<Status::Code>(value));
        break;
      }

      case RpcPacket::Fields::kCallId:
        // A decode error will propagate from Next() and terminate the loop.
        decoder.ReadUint32(&packet.call_id_).IgnoreError();
        break;
    }
  }

  if (status.IsDataLoss()) {
    return status;
  }

  return packet;
}

Result<ConstByteSpan> Packet::Encode(ByteSpan buffer) const {
  RpcPacket::MemoryEncoder rpc_packet(buffer);

  // The payload is encoded first, as it may share the encode buffer.
  if (!payload_.empty()) {
    rpc_packet.WritePayload(payload_).IgnoreError();
  }

  rpc_packet.WriteType(type_).IgnoreError();
  rpc_packet.WriteChannelId(channel_id_).IgnoreError();
  rpc_packet.WriteServiceId(service_id_).IgnoreError();
  rpc_packet.WriteMethodId(method_id_).IgnoreError();

  // Status code 0 is OK. In protobufs, 0 is the default int value, so skip
  // encoding it to save two bytes in the output.
  if (status_.code() != 0) {
    rpc_packet.WriteStatus(status_.code()).IgnoreError();
  }

  if (call_id_ != 0) {
    rpc_packet.WriteCallId(call_id_).IgnoreError();
  }

  if (rpc_packet.status().ok()) {
    return ConstByteSpan(rpc_packet);
  }
  return rpc_packet.status();
}

size_t Packet::MinEncodedSizeBytes() const {
  size_t reserved_size = 0;

  reserved_size += 1;  // channel_id key
  reserved_size += varint::EncodedSize(channel_id());
  reserved_size += 1 + sizeof(uint32_t);  // service_id key and fixed32
  reserved_size += 1 + sizeof(uint32_t);  // method_id key and fixed32

  // Packet type always takes two bytes to encode (varint key + varint enum).
  reserved_size += 2;

  // Status field takes up to two bytes to encode (varint key + varint status).
  reserved_size += 2;

  // Payload field takes at least two bytes to encode (varint key + length).
  reserved_size += 2;

  return reserved_size;
}

}  // namespace pw::rpc::internal