• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2021 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/** Functions for working with pw_rpc packets. */
16
17import {Message} from 'google-protobuf';
18import {MethodDescriptorProto} from 'google-protobuf/google/protobuf/descriptor_pb';
19import * as packetPb from 'packet_proto_tspb/packet_proto_tspb_pb/pw_rpc/internal/packet_pb';
20import {Status} from '@pigweed/pw_status';
21
22// Channel, Service, Method
23type idSet = [number, number, number];
24
25export function decode(data: Uint8Array): packetPb.RpcPacket {
26  return packetPb.RpcPacket.deserializeBinary(data);
27}
28
29export function decodePayload(payload: Uint8Array, payloadType: any): Message {
30  const message = payloadType.deserializeBinary(payload);
31  return message;
32}
33
34export function forServer(packet: packetPb.RpcPacket): boolean {
35  return packet.getType() % 2 == 0;
36}
37
38export function encodeClientError(
39  packet: packetPb.RpcPacket,
40  status: Status
41): Uint8Array {
42  const errorPacket = new packetPb.RpcPacket();
43  errorPacket.setType(packetPb.PacketType.CLIENT_ERROR);
44  errorPacket.setChannelId(packet.getChannelId());
45  errorPacket.setMethodId(packet.getMethodId());
46  errorPacket.setServiceId(packet.getServiceId());
47  errorPacket.setStatus(status);
48  return errorPacket.serializeBinary();
49}
50
51export function encodeClientStream(ids: idSet, message: Message): Uint8Array {
52  const streamPacket = new packetPb.RpcPacket();
53  streamPacket.setType(packetPb.PacketType.CLIENT_STREAM);
54  streamPacket.setChannelId(ids[0]);
55  streamPacket.setServiceId(ids[1]);
56  streamPacket.setMethodId(ids[2]);
57  streamPacket.setPayload(message.serializeBinary());
58  return streamPacket.serializeBinary();
59}
60
61export function encodeClientStreamEnd(ids: idSet): Uint8Array {
62  const streamEnd = new packetPb.RpcPacket();
63  streamEnd.setType(packetPb.PacketType.CLIENT_STREAM_END);
64  streamEnd.setChannelId(ids[0]);
65  streamEnd.setServiceId(ids[1]);
66  streamEnd.setMethodId(ids[2]);
67  return streamEnd.serializeBinary();
68}
69
70export function encodeRequest(ids: idSet, request?: Message): Uint8Array {
71  const payload: Uint8Array =
72    typeof request !== 'undefined'
73      ? request.serializeBinary()
74      : new Uint8Array();
75
76  const packet = new packetPb.RpcPacket();
77  packet.setType(packetPb.PacketType.REQUEST);
78  packet.setChannelId(ids[0]);
79  packet.setServiceId(ids[1]);
80  packet.setMethodId(ids[2]);
81  packet.setPayload(payload);
82  return packet.serializeBinary();
83}
84
85export function encodeResponse(ids: idSet, response: Message): Uint8Array {
86  const packet = new packetPb.RpcPacket();
87  packet.setType(packetPb.PacketType.RESPONSE);
88  packet.setChannelId(ids[0]);
89  packet.setServiceId(ids[1]);
90  packet.setMethodId(ids[2]);
91  packet.setPayload(response.serializeBinary());
92  return packet.serializeBinary();
93}
94
95export function encodeCancel(ids: idSet): Uint8Array {
96  const packet = new packetPb.RpcPacket();
97  packet.setType(packetPb.PacketType.CLIENT_ERROR);
98  packet.setStatus(Status.CANCELLED);
99  packet.setChannelId(ids[0]);
100  packet.setServiceId(ids[1]);
101  packet.setMethodId(ids[2]);
102  return packet.serializeBinary();
103}
104