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
15 #include <array>
16
17 #include "pb_decode.h"
18 #include "pb_encode.h"
19 #include "pw_rpc/nanopb/test_method_context.h"
20 #include "pw_rpc/service.h"
21 #include "pw_rpc_test_protos/test.rpc.pb.h"
22 #include "pw_unit_test/framework.h"
23
24 namespace pw::rpc {
25 namespace {
26
27 class TestServiceImpl final
28 : public test::pw_rpc::nanopb::TestService::Service<TestServiceImpl> {
29 public:
TestUnaryRpc(const pw_rpc_test_TestRequest &,pw_rpc_test_TestResponse &)30 Status TestUnaryRpc(const pw_rpc_test_TestRequest&,
31 pw_rpc_test_TestResponse&) {
32 return OkStatus();
33 }
34
TestAnotherUnaryRpc(const pw_rpc_test_TestRequest &,pw_rpc_test_TestResponse & response)35 Status TestAnotherUnaryRpc(const pw_rpc_test_TestRequest&,
36 pw_rpc_test_TestResponse& response) {
37 using ArgType = std::array<uint32_t, 3>;
38 // The values array needs to be kept in memory until after this method call
39 // returns since the response is not encoded until after returning from this
40 // method.
41 static const ArgType values = {7, 8, 9};
42 response.repeated_field.funcs.encode = +[](pb_ostream_t* stream,
43 const pb_field_t* field,
44 void* const* arg) -> bool {
45 // Note: nanopb passes the pointer to the repeated_filed.arg member as
46 // arg, not its contents.
47 for (auto elem : *static_cast<const ArgType*>(*arg)) {
48 if (!pb_encode_tag_for_field(stream, field) ||
49 !pb_encode_varint(stream, elem))
50 return false;
51 }
52 return true;
53 };
54 response.repeated_field.arg = const_cast<ArgType*>(&values);
55 return OkStatus();
56 }
57
TestServerStreamRpc(const pw_rpc_test_TestRequest &,NanopbServerWriter<pw_rpc_test_TestStreamResponse> &)58 void TestServerStreamRpc(
59 const pw_rpc_test_TestRequest&,
60 NanopbServerWriter<pw_rpc_test_TestStreamResponse>&) {}
61
TestClientStreamRpc(NanopbServerReader<pw_rpc_test_TestRequest,pw_rpc_test_TestStreamResponse> &)62 void TestClientStreamRpc(
63 NanopbServerReader<pw_rpc_test_TestRequest,
64 pw_rpc_test_TestStreamResponse>&) {}
65
TestBidirectionalStreamRpc(NanopbServerReaderWriter<pw_rpc_test_TestRequest,pw_rpc_test_TestStreamResponse> &)66 void TestBidirectionalStreamRpc(
67 NanopbServerReaderWriter<pw_rpc_test_TestRequest,
68 pw_rpc_test_TestStreamResponse>&) {}
69 };
70
TEST(NanopbTestMethodContext,ResponseWithCallbacks)71 TEST(NanopbTestMethodContext, ResponseWithCallbacks) {
72 PW_NANOPB_TEST_METHOD_CONTEXT(TestServiceImpl, TestAnotherUnaryRpc) ctx;
73 ASSERT_EQ(ctx.call(pw_rpc_test_TestRequest_init_default), OkStatus());
74
75 // Calling response() without an argument returns a Response struct from a
76 // newly decoded one, without any callbacks set.
77 EXPECT_EQ(ctx.response().repeated_field.arg, nullptr);
78
79 // To decode a response object that requires to set pb_callback_t members,
80 // pass it to the response() method as a parameter.
81 constexpr size_t kMaxNumValues = 4;
82 struct DecoderContext {
83 uint32_t num_calls = 0;
84 uint32_t values[kMaxNumValues];
85 bool failed = false;
86 } decoder_context;
87
88 pw_rpc_test_TestResponse response = pw_rpc_test_TestResponse_init_default;
89 response.repeated_field.funcs.decode = +[](pb_istream_t* stream,
90 const pb_field_t* /* field */,
91 void** arg) -> bool {
92 DecoderContext* dec_ctx = static_cast<DecoderContext*>(*arg);
93 uint64_t value;
94 if (!pb_decode_varint(stream, &value)) {
95 dec_ctx->failed = true;
96 return false;
97 }
98 if (dec_ctx->num_calls < kMaxNumValues) {
99 dec_ctx->values[dec_ctx->num_calls] = value;
100 }
101 dec_ctx->num_calls++;
102 return true;
103 };
104 response.repeated_field.arg = &decoder_context;
105 ctx.response(response);
106
107 EXPECT_FALSE(decoder_context.failed);
108 EXPECT_EQ(3u, decoder_context.num_calls);
109 EXPECT_EQ(7u, decoder_context.values[0]);
110 EXPECT_EQ(8u, decoder_context.values[1]);
111 EXPECT_EQ(9u, decoder_context.values[2]);
112 }
113
114 } // namespace
115 } // namespace pw::rpc
116