1 // Copyright 2021 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 "pw_assert/assert.h"
16 #include "pw_bloat/bloat_this_binary.h"
17 #include "pw_log/log.h"
18 #include "pw_router/egress_function.h"
19 #include "pw_router/static_router.h"
20 #include "pw_sys_io/sys_io.h"
25 static constexpr uint32_t kMagic = 0x8badf00d;
27 constexpr BasicPacket(uint32_t addr, uint64_t data)
28 : magic(kMagic), address(addr), payload(data) {}
37 // All the new router-specific stuff.
40 class BasicPacketParser : public pw::router::PacketParser {
42 constexpr BasicPacketParser() : packet_(nullptr) {}
44 bool Parse(pw::ConstByteSpan packet) final {
45 packet_ = reinterpret_cast<const BasicPacket*>(packet.data());
46 return packet_->magic == BasicPacket::kMagic;
49 std::optional<uint32_t> GetDestinationAddress() const final {
50 return packet_->address;
54 const BasicPacket* packet_;
57 BasicPacketParser parser;
58 pw::router::EgressFunction sys_io_egress(+[](pw::ConstByteSpan packet) {
59 return pw::sys_io::WriteBytes(packet).status();
61 constexpr pw::router::StaticRouter::Route routes[] = {{1, sys_io_egress}};
62 pw::router::StaticRouter router(parser, routes);
67 pw::bloat::BloatThisBinary();
69 // Ensure we are paying the cost for log and assert.
70 BasicPacket packet(0x1, 0x2);
71 PW_CHECK_UINT_EQ(packet.magic, BasicPacket::kMagic, "Some CHECK logic");
72 PW_LOG_INFO("Packet has address %u", static_cast<unsigned>(packet.address));
73 PW_LOG_INFO("pw_StatusString %s", pw::OkStatus().str());
75 std::array<std::byte, sizeof(BasicPacket)> packet_buffer;
76 pw::sys_io::ReadBytes(packet_buffer);
77 pw::sys_io::WriteBytes(packet_buffer);
80 pw::sys_io::ReadBytes(packet_buffer);
81 router.RoutePacket(packet_buffer);
84 return static_cast<int>(packet.payload);