1 // Copyright 2020 The Pigweed Authors
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
7 // https://www.apache.org/licenses/LICENSE-2.0
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
15 #include "pb_decode.h"
16 #include "pb_encode.h"
17 #include "pw_assert/assert.h"
18 #include "pw_bloat/bloat_this_binary.h"
19 #include "pw_log/log.h"
20 #include "pw_sys_io/sys_io.h"
22 int volatile* unoptimizable;
24 namespace my_product {
26 template <typename DecodeFunction>
29 template <typename FieldsType>
30 struct NanopbTraits<bool(pb_istream_t*, FieldsType, void*)> {
31 using Fields = FieldsType;
34 using Fields = typename NanopbTraits<decltype(pb_decode)>::Fields;
36 // Performs the core nanopb encode and decode operations so that those functions
37 // are included in the binary.
38 void DoNanopbStuff() {
39 std::byte buffer[128];
40 void* fields = &buffer;
42 auto output = pb_ostream_from_buffer(reinterpret_cast<pb_byte_t*>(buffer),
44 pb_encode(&output, static_cast<Fields>(fields), buffer);
46 auto input = pb_istream_from_buffer(
47 reinterpret_cast<const pb_byte_t*>(buffer), sizeof(buffer));
48 pb_decode(&input, static_cast<Fields>(fields), buffer);
51 } // namespace my_product
54 pw::bloat::BloatThisBinary();
55 my_product::DoNanopbStuff();
57 // Ensure we are paying the cost for log and assert.
58 PW_CHECK_INT_GE(*unoptimizable, 0, "Ensure this CHECK logic stays");
59 PW_LOG_INFO("We care about optimizing: %d", *unoptimizable);
61 std::byte packet_buffer[128];
62 pw::sys_io::ReadBytes(packet_buffer);
63 pw::sys_io::WriteBytes(packet_buffer);
65 return static_cast<int>(packet_buffer[92]);