• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "pb_decode.h"
16 #include "pb_encode.h"
17 #include "pw_assert/check.h"
18 #include "pw_bloat/bloat_this_binary.h"
19 #include "pw_log/log.h"
20 #include "pw_rpc/echo_service_nanopb.h"
21 #include "pw_rpc/server.h"
22 #include "pw_sys_io/sys_io.h"
23 
24 int volatile* unoptimizable;
25 
26 class Output : public pw::rpc::ChannelOutput {
27  public:
Output()28   Output() : ChannelOutput("output") {}
29 
Send(pw::span<const std::byte> buffer)30   pw::Status Send(pw::span<const std::byte> buffer) override {
31     return pw::sys_io::WriteBytes(buffer).status();
32   }
33 };
34 
35 namespace my_product {
36 
37 template <typename DecodeFunction>
38 struct NanopbTraits;
39 
40 template <typename FieldsType>
41 struct NanopbTraits<bool(pb_istream_t*, FieldsType, void*)> {
42   using Fields = FieldsType;
43 };
44 
45 using Fields = typename NanopbTraits<decltype(pb_decode)>::Fields;
46 
47 // Performs the core nanopb encode and decode operations so that those functions
48 // are included in the binary.
DoNanopbStuff()49 void DoNanopbStuff() {
50   std::byte buffer[128];
51   void* fields = &buffer;
52 
53   auto output = pb_ostream_from_buffer(reinterpret_cast<pb_byte_t*>(buffer),
54                                        sizeof(buffer));
55   pb_encode(&output, static_cast<Fields>(fields), buffer);
56 
57   auto input = pb_istream_from_buffer(
58       reinterpret_cast<const pb_byte_t*>(buffer), sizeof(buffer));
59   pb_decode(&input, static_cast<Fields>(fields), buffer);
60 }
61 
62 Output output;
63 pw::rpc::Channel channels[] = {pw::rpc::Channel::Create<1>(&output)};
64 pw::rpc::Server server(channels);
65 pw::rpc::EchoService echo_service;
66 
67 }  // namespace my_product
68 
main()69 int main() {
70   pw::bloat::BloatThisBinary();
71   my_product::DoNanopbStuff();
72 
73   // Ensure we are paying the cost for log and assert.
74   PW_CHECK_INT_GE(*unoptimizable, 0, "Ensure this CHECK logic stays");
75   PW_LOG_INFO("We care about optimizing: %d", *unoptimizable);
76 
77   std::byte packet_buffer[128];
78   pw::sys_io::ReadBytes(packet_buffer);
79   pw::sys_io::WriteBytes(packet_buffer);
80 
81   my_product::server.RegisterService(my_product::echo_service);
82   my_product::server.ProcessPacket(packet_buffer);
83 
84   return static_cast<int>(packet_buffer[92]);
85 }
86