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