MySensors Library & Examples  2.3.2-62-ge298769
PJONDynamicRouter.h
1 
2 /*-O//\ __ __
3  |-gfo\ |__| | | | |\ | ®
4  |!y°o:\ | __| |__| | \| 13.0
5  |y"s§+`\ multi-master, multi-media bus network protocol
6  /so+:-..`\ Copyright 2010-2020 by Giovanni Blu Mitolo [email protected]
7  |+/:ngr-*.`\
8  |5/:%&-a3f.:;\
9  \+//u/+g%{osv,,\
10  \=+&/osw+olds.\\
11  \:/+-.-°-:+oss\
12  | | \oy\\
13  > <
14 ______-| |-__________________________________________________________________
15 
16 PJONDynamicRouter has been contributed by Fred Larsen.
17 
18 It performs the same as PJONRouter, but populates the routing table
19 dynamically based on observed packets from remote buses.
20 _____________________________________________________________________________
21 
22 This software is experimental and it is distributed "AS IS" without any
23 warranty, use it at your own risk.
24 
25 Copyright 2010-2020 by Giovanni Blu Mitolo [email protected]
26 
27 Licensed under the Apache License, Version 2.0 (the "License");
28 you may not use this file except in compliance with the License.
29 You may obtain a copy of the License at
30 
31  http://www.apache.org/licenses/LICENSE-2.0
32 
33 Unless required by applicable law or agreed to in writing, software
34 distributed under the License is distributed on an "AS IS" BASIS,
35 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 See the License for the specific language governing permissions and
37 limitations under the License. */
38 
39 #pragma once
40 
41 #ifndef PJON_ROUTER_TABLE_SIZE
42 #define PJON_ROUTER_TABLE_SIZE 100
43 #endif
44 
45 #include "PJONRouter.h"
46 
48 {
49 protected:
50 
51  void add_sender_to_routing_table(
52  const PJON_Packet_Info &packet_info,
53  uint8_t sender_bus
54  )
55  {
56  uint8_t start_search = 0;
57  uint8_t found_bus = find_bus_with_id(
58  packet_info.tx.bus_id,
59  packet_info.tx.id,
60  start_search
61  );
62  // Not found among attached buses or in routing table. Add to table.
63  if(found_bus == PJON_NOT_ASSIGNED) {
64  add(packet_info.tx.bus_id, sender_bus);
65  }
66  };
67 
68  virtual void dynamic_receiver_function(
69  uint8_t *payload,
70  uint16_t length,
71  const PJON_Packet_Info &packet_info
72  )
73  {
74  // Do standard routing but also add unknown remote buses to routing table
75  add_sender_to_routing_table(packet_info, current_bus);
76  PJONSwitch::dynamic_receiver_function(payload, length, packet_info);
77  };
78 
79 public:
80  PJONDynamicRouter() { };
81 
83  uint8_t bus_count,
84  PJONAny * const buses[],
85  uint8_t default_gateway = PJON_NOT_ASSIGNED
86  ) : PJONRouter(bus_count, buses, default_gateway) { };
87 };
88 
89 // Specialized class to simplify declaration when using 2 buses
90 template<class A, class B>
92 {
93  StrategyLink<A> linkA;
94  StrategyLink<B> linkB;
95  PJONAny busA, busB;
96 public:
97  PJONDynamicRouter2(uint8_t default_gateway = PJON_NOT_ASSIGNED)
98  {
99  PJON<Any>* buses[2] = { &busA, &busB };
100  PJONSimpleSwitch<Any>::connect_buses(2, buses, default_gateway);
101  busA.set_link(&linkA);
102  busB.set_link(&linkB);
103  };
104 
105  PJONAny &get_bus(const uint8_t ix)
106  {
107  return ix == 0 ? busA : busB;
108  }
109 
110  A &get_strategy_0()
111  {
112  return linkA.strategy;
113  }
114  B &get_strategy_1()
115  {
116  return linkB.strategy;
117  }
118 };
119 
120 // Specialized class to simplify declaration when using 3 buses
121 template<class A, class B, class C>
123 {
124  StrategyLink<A> linkA;
125  StrategyLink<B> linkB;
126  StrategyLink<C> linkC;
127  PJONAny busA, busB, busC;
128 public:
129  PJONDynamicRouter3(uint8_t default_gateway = PJON_NOT_ASSIGNED)
130  {
131  PJON<Any> *buses[3] = { &busA, &busB, &busC };
132  PJONSimpleSwitch<Any>::connect_buses(3, buses, default_gateway);
133  busA.set_link(&linkA);
134  busB.set_link(&linkB);
135  busC.set_link(&linkC);
136  };
137 
138  PJONAny &get_bus(const uint8_t ix)
139  {
140  return ix == 0 ? busA : (ix == 1 ? busB : busC);
141  }
142 
143  A &get_strategy_0()
144  {
145  return linkA.strategy;
146  }
147  B &get_strategy_1()
148  {
149  return linkB.strategy;
150  }
151  C &get_strategy_2()
152  {
153  return linkC.strategy;
154  }
155 };
PJONDynamicRouter2
Definition: PJONDynamicRouter.h:91
PJONRouter
Definition: PJONRouter.h:51
PJON_Packet_Info
Definition: PJONDefines.h:207
PJONDynamicRouter
Definition: PJONDynamicRouter.h:47
PJONAny
Definition: PJONSwitch.h:44
PJON< Any >
PJONSimpleSwitch
Definition: PJONSimpleSwitch.h:63
PJONDynamicRouter3
Definition: PJONDynamicRouter.h:122