Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 1 | // Copyright 2020 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 | |
| 15 | #include "pw_rpc/internal/packet.h" |
| 16 | |
| 17 | #include "pw_protobuf/decoder.h" |
| 18 | |
| 19 | namespace pw::rpc::internal { |
| 20 | |
Wyatt Hepler | 142616c | 2020-06-01 10:16:04 -0700 | [diff] [blame] | 21 | using std::byte; |
| 22 | |
Wyatt Hepler | 712d367 | 2020-07-13 15:52:11 -0700 | [diff] [blame^] | 23 | Status Packet::FromBuffer(std::span<const byte> data, Packet& packet) { |
| 24 | packet = Packet(); |
Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 25 | |
| 26 | protobuf::Decoder decoder(data); |
| 27 | |
Wyatt Hepler | 712d367 | 2020-07-13 15:52:11 -0700 | [diff] [blame^] | 28 | Status status; |
| 29 | |
| 30 | while ((status = decoder.Next()).ok()) { |
Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 31 | RpcPacket::Fields field = |
| 32 | static_cast<RpcPacket::Fields>(decoder.FieldNumber()); |
Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 33 | |
| 34 | switch (field) { |
Wyatt Hepler | 712d367 | 2020-07-13 15:52:11 -0700 | [diff] [blame^] | 35 | case RpcPacket::Fields::TYPE: { |
| 36 | uint32_t value; |
| 37 | decoder.ReadUint32(&value); |
| 38 | packet.set_type(static_cast<PacketType>(value)); |
Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 39 | break; |
Wyatt Hepler | 712d367 | 2020-07-13 15:52:11 -0700 | [diff] [blame^] | 40 | } |
Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 41 | |
Alexei Frolov | d54975b | 2020-05-19 15:13:27 -0700 | [diff] [blame] | 42 | case RpcPacket::Fields::CHANNEL_ID: |
Wyatt Hepler | 712d367 | 2020-07-13 15:52:11 -0700 | [diff] [blame^] | 43 | decoder.ReadUint32(&packet.channel_id_); |
Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 44 | break; |
| 45 | |
Alexei Frolov | d54975b | 2020-05-19 15:13:27 -0700 | [diff] [blame] | 46 | case RpcPacket::Fields::SERVICE_ID: |
Wyatt Hepler | 712d367 | 2020-07-13 15:52:11 -0700 | [diff] [blame^] | 47 | decoder.ReadFixed32(&packet.service_id_); |
Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 48 | break; |
| 49 | |
Alexei Frolov | d54975b | 2020-05-19 15:13:27 -0700 | [diff] [blame] | 50 | case RpcPacket::Fields::METHOD_ID: |
Wyatt Hepler | 712d367 | 2020-07-13 15:52:11 -0700 | [diff] [blame^] | 51 | decoder.ReadFixed32(&packet.method_id_); |
Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 52 | break; |
| 53 | |
Alexei Frolov | d54975b | 2020-05-19 15:13:27 -0700 | [diff] [blame] | 54 | case RpcPacket::Fields::PAYLOAD: |
Wyatt Hepler | 712d367 | 2020-07-13 15:52:11 -0700 | [diff] [blame^] | 55 | decoder.ReadBytes(&packet.payload_); |
Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 56 | break; |
Alexei Frolov | 0089094 | 2020-05-19 10:45:08 -0700 | [diff] [blame] | 57 | |
Wyatt Hepler | 712d367 | 2020-07-13 15:52:11 -0700 | [diff] [blame^] | 58 | case RpcPacket::Fields::STATUS: { |
| 59 | uint32_t value; |
Alexei Frolov | 0089094 | 2020-05-19 10:45:08 -0700 | [diff] [blame] | 60 | decoder.ReadUint32(&value); |
Wyatt Hepler | 712d367 | 2020-07-13 15:52:11 -0700 | [diff] [blame^] | 61 | packet.set_status(static_cast<Status::Code>(value)); |
Alexei Frolov | 0089094 | 2020-05-19 10:45:08 -0700 | [diff] [blame] | 62 | break; |
Wyatt Hepler | 712d367 | 2020-07-13 15:52:11 -0700 | [diff] [blame^] | 63 | } |
Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
Wyatt Hepler | 712d367 | 2020-07-13 15:52:11 -0700 | [diff] [blame^] | 67 | return status == Status::DATA_LOSS ? Status::DATA_LOSS : Status::OK; |
Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Wyatt Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame] | 70 | StatusWithSize Packet::Encode(std::span<byte> buffer) const { |
Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 71 | pw::protobuf::NestedEncoder encoder(buffer); |
| 72 | RpcPacket::Encoder rpc_packet(&encoder); |
| 73 | |
Alexei Frolov | 33a1e8f | 2020-05-26 08:39:32 -0700 | [diff] [blame] | 74 | // The payload is encoded first, as it may share the encode buffer. |
| 75 | rpc_packet.WritePayload(payload_); |
| 76 | |
Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 77 | rpc_packet.WriteType(type_); |
| 78 | rpc_packet.WriteChannelId(channel_id_); |
| 79 | rpc_packet.WriteServiceId(service_id_); |
| 80 | rpc_packet.WriteMethodId(method_id_); |
Alexei Frolov | 0089094 | 2020-05-19 10:45:08 -0700 | [diff] [blame] | 81 | rpc_packet.WriteStatus(status_); |
Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 82 | |
Wyatt Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame] | 83 | std::span<const byte> proto; |
Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 84 | if (Status status = encoder.Encode(&proto); !status.ok()) { |
| 85 | return StatusWithSize(status, 0); |
| 86 | } |
| 87 | |
| 88 | return StatusWithSize(proto.size()); |
| 89 | } |
| 90 | |
Wyatt Hepler | 671946e | 2020-06-09 14:39:33 -0700 | [diff] [blame] | 91 | size_t Packet::MinEncodedSizeBytes() const { |
Wyatt Hepler | 142616c | 2020-06-01 10:16:04 -0700 | [diff] [blame] | 92 | size_t reserved_size = 0; |
| 93 | |
| 94 | reserved_size += 1; // channel_id key |
| 95 | reserved_size += varint::EncodedSize(channel_id()); |
Wyatt Hepler | 712d367 | 2020-07-13 15:52:11 -0700 | [diff] [blame^] | 96 | reserved_size += 1 + sizeof(uint32_t); // service_id key and fixed32 |
| 97 | reserved_size += 1 + sizeof(uint32_t); // method_id key and fixed32 |
Wyatt Hepler | 142616c | 2020-06-01 10:16:04 -0700 | [diff] [blame] | 98 | |
| 99 | // Packet type always takes two bytes to encode (varint key + varint enum). |
| 100 | reserved_size += 2; |
| 101 | |
| 102 | // Status field always takes two bytes to encode (varint key + varint status). |
| 103 | reserved_size += 2; |
| 104 | |
| 105 | // Payload field takes at least two bytes to encode (varint key + length). |
| 106 | reserved_size += 2; |
| 107 | |
Wyatt Hepler | 671946e | 2020-06-09 14:39:33 -0700 | [diff] [blame] | 108 | return reserved_size; |
Wyatt Hepler | 142616c | 2020-06-01 10:16:04 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 111 | } // namespace pw::rpc::internal |