Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_router / public / pw_router / static_router.h
1 // Copyright 2021 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 #pragma once
15
16 #include <span>
17
18 #include "pw_bytes/span.h"
19 #include "pw_metric/metric.h"
20 #include "pw_router/egress.h"
21 #include "pw_router/packet_parser.h"
22 #include "pw_status/status.h"
23 #include "pw_sync/mutex.h"
24
25 namespace pw::router {
26
27 // A packet router with a static routing table.
28 //
29 // Thread-safety:
30 //   Internal packet parsing and calls to the provided PacketParser are
31 //   synchronized. Synchronization at the egress level must be implemented by
32 //   derived egresses.
33 //
34 class StaticRouter {
35  public:
36   struct Route {
37     // TODO(frolv): Consider making address size configurable.
38     uint32_t address;
39     Egress& egress;
40   };
41
42   StaticRouter(PacketParser& parser, std::span<const Route> routes)
43       : parser_(parser), routes_(routes) {}
44
45   StaticRouter(const StaticRouter&) = delete;
46   StaticRouter(StaticRouter&&) = delete;
47   StaticRouter& operator=(const StaticRouter&) = delete;
48   StaticRouter& operator=(StaticRouter&&) = delete;
49
50   uint32_t dropped_packets() const {
51     return parser_errors_.value() + route_errors_.value() +
52            egress_errors_.value();
53   }
54
55   const metric::Group& metrics() { return metrics_; }
56
57   // Routes a single packet through the appropriate egress.
58   // Returns one of the following to indicate a router-side error:
59   //
60   //   OK - Packet sent successfully.
61   //   DATA_LOSS - Packet corrupt or incomplete.
62   //   NOT_FOUND - No registered route for the packet.
63   //   UNAVAILABLE - Route egress did not accept packet.
64   //
65   Status RoutePacket(ConstByteSpan packet);
66
67  private:
68   PacketParser& parser_;
69   std::span<const Route> routes_;
70   sync::Mutex mutex_;
71   PW_METRIC_GROUP(metrics_, "static_router");
72   PW_METRIC(metrics_, parser_errors_, "parser_errors", 0u);
73   PW_METRIC(metrics_, route_errors_, "route_errors", 0u);
74   PW_METRIC(metrics_, egress_errors_, "egress_errors", 0u);
75 };
76
77 }  // namespace pw::router