• 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"""This module generates the code for raw pw_rpc services."""
15
16import os
17from typing import Iterable
18
19from pw_protobuf.output_file import OutputFile
20from pw_protobuf.proto_tree import ProtoServiceMethod
21from pw_protobuf.proto_tree import build_node_tree
22from pw_rpc import codegen
23from pw_rpc.codegen import (
24    client_call_type,
25    get_id,
26    CodeGenerator,
27    RPC_NAMESPACE,
28)
29
30PROTO_H_EXTENSION = '.pb.h'
31
32
33def _proto_filename_to_generated_header(proto_file: str) -> str:
34    """Returns the generated C++ RPC header name for a .proto file."""
35    filename = os.path.splitext(proto_file)[0]
36    return f'{filename}.raw_rpc{PROTO_H_EXTENSION}'
37
38
39def _proto_filename_to_stub_header(proto_file: str) -> str:
40    """Returns the generated C++ RPC header name for a .proto file."""
41    filename = os.path.splitext(proto_file)[0]
42    return f'{filename}.raw_rpc.stub{PROTO_H_EXTENSION}'
43
44
45def _function(method: ProtoServiceMethod) -> str:
46    return f'{client_call_type(method, "Raw")} {method.name()}'
47
48
49def _user_args(method: ProtoServiceMethod) -> Iterable[str]:
50    if not method.client_streaming():
51        yield '::pw::ConstByteSpan request'
52
53    if method.server_streaming():
54        yield '::pw::Function<void(::pw::ConstByteSpan)>&& on_next = nullptr'
55        yield '::pw::Function<void(::pw::Status)>&& on_completed = nullptr'
56    else:
57        yield (
58            '::pw::Function<void(::pw::ConstByteSpan, ::pw::Status)>&& '
59            'on_completed = nullptr'
60        )
61
62    yield '::pw::Function<void(::pw::Status)>&& on_error = nullptr'
63
64
65class RawCodeGenerator(CodeGenerator):
66    """Generates an RPC service and client using the raw buffers API."""
67
68    def name(self) -> str:
69        return 'raw'
70
71    def method_union_name(self) -> str:
72        return 'RawMethodUnion'
73
74    def includes(self, unused_proto_file_name: str) -> Iterable[str]:
75        yield '#include "pw_rpc/raw/client_reader_writer.h"'
76        yield '#include "pw_rpc/raw/internal/method_union.h"'
77        yield '#include "pw_rpc/raw/server_reader_writer.h"'
78
79    def service_aliases(self) -> None:
80        self.line(f'using RawServerWriter = {RPC_NAMESPACE}::RawServerWriter;')
81        self.line(f'using RawServerReader = {RPC_NAMESPACE}::RawServerReader;')
82        self.line(
83            'using RawServerReaderWriter = '
84            f'{RPC_NAMESPACE}::RawServerReaderWriter;'
85        )
86
87    def method_descriptor(self, method: ProtoServiceMethod) -> None:
88        impl_method = f'&Implementation::{method.name()}'
89
90        self.line(
91            f'{RPC_NAMESPACE}::internal::GetRawMethodFor<{impl_method}, '
92            f'{method.type().cc_enum()}>('
93        )
94        self.line(f'    {get_id(method)}),  // Hash of "{method.name()}"')
95
96    def client_member_function(self, method: ProtoServiceMethod) -> None:
97        self.line(f'{_function(method)}(')
98        self.indented_list(*_user_args(method), end=') const {')
99
100        with self.indent():
101            base = 'Stream' if method.server_streaming() else 'Unary'
102            self.line(
103                f'return {RPC_NAMESPACE}::internal::'
104                f'{base}ResponseClientCall::'
105                f'Start<{client_call_type(method, "Raw")}>('
106            )
107
108            service_client = RPC_NAMESPACE + '::internal::ServiceClient'
109            arg = ['std::move(on_next)'] if method.server_streaming() else []
110
111            self.indented_list(
112                f'{service_client}::client()',
113                f'{service_client}::channel_id()',
114                'kServiceId',
115                get_id(method),
116                *arg,
117                'std::move(on_completed)',
118                'std::move(on_error)',
119                '{}' if method.client_streaming() else 'request',
120                end=');',
121            )
122
123        self.line('}')
124
125    def client_static_function(self, method: ProtoServiceMethod) -> None:
126        self.line(f'static {_function(method)}(')
127        self.indented_list(
128            f'{RPC_NAMESPACE}::Client& client',
129            'uint32_t channel_id',
130            *_user_args(method),
131            end=') {',
132        )
133
134        with self.indent():
135            self.line(f'return Client(client, channel_id).{method.name()}(')
136
137            args = []
138
139            if not method.client_streaming():
140                args.append('request')
141
142            if method.server_streaming():
143                args.append('std::move(on_next)')
144
145            self.indented_list(
146                *args,
147                'std::move(on_completed)',
148                'std::move(on_error)',
149                end=');',
150            )
151
152        self.line('}')
153
154    def method_info_specialization(self, method: ProtoServiceMethod) -> None:
155        self.line()
156        # We have Request/Response as voids to mark raw as a special case.
157        # Raw operates in ConstByteSpans, which won't be copied by copying the
158        # span itself and without special treatment will lead to dangling
159        # pointers.
160        #
161        # Helpers/traits that want to use Request/Response and should support
162        # raw are required to do a special implementation for them instead that
163        # will copy the actual data.
164        self.line('using Request = void;')
165        self.line('using Response = void;')
166
167
168class StubGenerator(codegen.StubGenerator):
169    """TODO(frolv) Add docstring."""
170
171    def unary_signature(self, method: ProtoServiceMethod, prefix: str) -> str:
172        return (
173            f'void {prefix}{method.name()}(pw::ConstByteSpan request, '
174            'pw::rpc::RawUnaryResponder& responder)'
175        )
176
177    def unary_stub(
178        self, method: ProtoServiceMethod, output: OutputFile
179    ) -> None:
180        output.write_line(codegen.STUB_REQUEST_TODO)
181        output.write_line('static_cast<void>(request);')
182        output.write_line(codegen.STUB_RESPONSE_TODO)
183        output.write_line('static_cast<void>(responder);')
184
185    def server_streaming_signature(
186        self, method: ProtoServiceMethod, prefix: str
187    ) -> str:
188        return (
189            f'void {prefix}{method.name()}('
190            'pw::ConstByteSpan request, RawServerWriter& writer)'
191        )
192
193    def client_streaming_signature(
194        self, method: ProtoServiceMethod, prefix: str
195    ) -> str:
196        return f'void {prefix}{method.name()}(RawServerReader& reader)'
197
198    def bidirectional_streaming_signature(
199        self, method: ProtoServiceMethod, prefix: str
200    ) -> str:
201        return (
202            f'void {prefix}{method.name()}('
203            'RawServerReaderWriter& reader_writer)'
204        )
205
206
207def process_proto_file(proto_file) -> Iterable[OutputFile]:
208    """Generates code for a single .proto file."""
209
210    _, package_root = build_node_tree(proto_file)
211    output_filename = _proto_filename_to_generated_header(proto_file.name)
212
213    generator = RawCodeGenerator(output_filename)
214    codegen.generate_package(proto_file, package_root, generator)
215
216    codegen.package_stubs(package_root, generator, StubGenerator())
217
218    return [generator.output]
219