58 #ifndef PJON_ROUTER_MAX_BUSES
59 #define PJON_ROUTER_MAX_BUSES 5
62 template<
class Strategy>
66 uint8_t bus_count = 0;
67 uint8_t default_gateway = PJON_NOT_ASSIGNED;
68 uint8_t current_bus = PJON_NOT_ASSIGNED;
74 uint8_t default_gateway_in,
76 PJON_Receiver receiver,
80 bus_count = (bus_count_in > PJON_ROUTER_MAX_BUSES) ?
81 PJON_ROUTER_MAX_BUSES : bus_count_in;
82 default_gateway = default_gateway_in;
83 for(uint8_t i = 0; i < bus_count; i++) {
84 buses[i] = buses_in[i];
85 buses[i]->set_receiver(receiver);
86 buses[i]->set_error(error);
87 buses[i]->set_custom_pointer(custom_pointer);
88 buses[i]->set_router(
true);
92 uint8_t find_attached_bus_with_id(
93 const uint8_t *bus_id,
98 for(uint8_t i=start_bus; i<bus_count; i++) {
99 if(memcmp(bus_id, buses[i]->tx.bus_id, 4) == 0) {
104 start_bus = PJON_NOT_ASSIGNED;
105 return PJON_NOT_ASSIGNED;
108 #ifdef PJON_ROUTER_NEED_INHERITANCE
111 void send_packet(
const uint8_t *payload,
const uint16_t length,
112 const uint8_t receiver_bus,
const uint8_t sender_bus,
118 (packet_info.header & PJON_ACK_REQ_BIT) &&
119 (packet_info.rx.id != PJON_BROADCAST)
121 buses[sender_bus]->strategy.send_response(PJON_ACK);
127 uint8_t send_bus = current_bus;
128 current_bus = receiver_bus;
134 if ((packet_info.header & PJON_MODE_BIT) &&
135 !(buses[sender_bus]->config & PJON_MODE_BIT) &&
136 memcmp(buses[sender_bus]->tx.bus_id, PJONTools::localhost(), 4)!=0 &&
137 memcmp(packet_info.tx.bus_id, PJONTools::localhost(), 4)==0) {
139 memcpy(&p_info.tx.bus_id, buses[sender_bus]->tx.bus_id, 4);
145 if ((packet_info.header & PJON_MODE_BIT) &&
146 !(buses[receiver_bus]->config & PJON_MODE_BIT) &&
147 memcmp(buses[receiver_bus]->tx.bus_id, PJONTools::localhost(), 4)!=0 &&
148 memcmp(packet_info.rx.bus_id, buses[receiver_bus]->tx.bus_id, 4)==0) {
150 memcpy(p_info.rx.bus_id, PJONTools::localhost(), 4);
154 #if PJON_MAX_PACKETS == 0
155 buses[receiver_bus]->forward_blocking(
157 (
const uint8_t *)payload,
162 if(result == PJON_FAIL) {
163 dynamic_error_function(PJON_CONNECTION_LOST, 0);
166 buses[receiver_bus]->forward(
168 (
const uint8_t *)payload,
172 current_bus = send_bus;
176 const uint8_t *payload,
177 const uint16_t length,
178 const uint8_t receiver_bus,
179 const uint8_t sender_bus,
185 if(receiver_bus != PJON_NOT_ASSIGNED && receiver_bus != sender_bus)
196 #ifdef PJON_ROUTER_NEED_INHERITANCE
199 uint8_t find_bus_with_id(
200 const uint8_t bus_id[],
201 const uint8_t device_id,
205 return find_attached_bus_with_id(bus_id, device_id, start_bus);
208 #ifdef PJON_ROUTER_NEED_INHERITANCE
211 void dynamic_receiver_function(
217 uint8_t start_search = 0;
218 bool ack_sent =
false;
220 uint8_t receiver_bus = find_bus_with_id((
const uint8_t*)
221 ((packet_info.header & PJON_MODE_BIT) != 0 ?
222 packet_info.rx.bus_id : PJONTools::localhost()),
223 packet_info.rx.id, start_search
236 if(receiver_bus == PJON_NOT_ASSIGNED) {
237 receiver_bus = default_gateway;
249 }
while(start_search != PJON_NOT_ASSIGNED);
252 #ifdef PJON_ROUTER_NEED_INHERITANCE
255 void dynamic_error_function(uint8_t code, uint16_t
data) { }
264 uint8_t default_gateway = PJON_NOT_ASSIGNED
267 connect_buses(bus_count, buses, default_gateway);
274 uint8_t default_gateway = PJON_NOT_ASSIGNED
278 connect_buses(2, buses, default_gateway);
286 uint8_t default_gateway = PJON_NOT_ASSIGNED
290 connect_buses(3, buses, default_gateway);
295 for(uint8_t i = 0; i < bus_count; i++) {
302 for(current_bus = 0; current_bus < bus_count; current_bus++) {
304 buses[current_bus]->receive(
305 buses[current_bus]->strategy.get_receive_time()
307 if(PJON_MAX_PACKETS < bus_count && code == PJON_ACK) {
311 for(current_bus = 0; current_bus < bus_count; current_bus++) {
312 buses[current_bus]->update();
314 current_bus = PJON_NOT_ASSIGNED;
318 uint8_t bus_count_in,
320 uint8_t default_gateway_in = PJON_NOT_ASSIGNED
335 uint8_t get_callback_bus()
const
346 static void receiver_function(
354 )->dynamic_receiver_function(
361 static void error_function(uint8_t code, uint16_t
data,
void *custom_pointer)