50 #include <PJONDynamicRouter.h>
51 #include <PJONVirtualBusRouter.h>
53 typedef void (* PJON_Send_Notification)(
54 const uint8_t *
const payload,
55 const uint16_t length,
56 const uint8_t receiver_bus,
57 const uint8_t sender_bus,
61 template<
class RouterClass = PJONSwitch>
65 void *custom_pointer = NULL;
66 PJON_Receiver receiver = NULL;
67 PJON_Error error = NULL;
68 PJON_Send_Notification send_notification = NULL;
71 virtual void dynamic_receiver_function(uint8_t *payload, uint16_t length,
77 bool packet_is_for_me = (
78 RouterClass::buses[RouterClass::current_bus]->tx.id != PJON_NOT_ASSIGNED &&
79 memcmp(RouterClass::buses[RouterClass::current_bus]->tx.bus_id, packet_info.rx.bus_id, 4) == 0 &&
80 RouterClass::buses[RouterClass::current_bus]->tx.id == packet_info.rx.id
84 if(!packet_is_for_me) {
85 RouterClass::dynamic_receiver_function(payload, length, packet_info);
86 }
else if(packet_info.header & PJON_ACK_REQ_BIT) {
87 RouterClass::buses[RouterClass::current_bus]->send_acknowledge();
90 if(router || packet_is_for_me) {
94 p_i.custom_pointer = custom_pointer;
96 receiver(payload, length, p_i);
101 virtual void dynamic_error_function(uint8_t code, uint16_t
data)
103 RouterClass::dynamic_error_function(code,
data);
107 error(code,
data, custom_pointer);
111 virtual void send_packet(
const uint8_t *payload,
const uint16_t length,
112 const uint8_t receiver_bus,
const uint8_t sender_bus,
115 RouterClass::send_packet(payload, length, receiver_bus, sender_bus, ack_sent, packet_info);
118 if (send_notification) {
119 send_notification(payload, length, receiver_bus, sender_bus, packet_info);
128 uint8_t default_gateway = PJON_NOT_ASSIGNED)
129 : RouterClass(bus_count, buses, default_gateway) {}
131 void set_receiver(PJON_Receiver r)
136 void set_error(PJON_Error e)
141 void set_send_notification(PJON_Send_Notification s)
143 send_notification = s;
146 void set_custom_ptr(
void *custom_ptr)
148 custom_pointer = custom_ptr;
152 const uint8_t *payload,
157 dynamic_receiver_function(payload, length, packet_info);
161 void set_router(
bool on)
168 template<
class A,
class B,
class RouterClass = PJONSwitch>
179 busA.set_link(&linkA);
180 busB.set_link(&linkB);
183 PJONAny &get_bus(
const uint8_t ix)
185 return ix == 0 ? busA : busB;
190 return linkA.strategy;
194 return linkB.strategy;
199 template<
class A,
class B,
class C,
class RouterClass = PJONSwitch>
209 PJON<Any>* buses[3] = { &busA, &busB, &busC };
211 busA.set_link(&linkA);
212 busB.set_link(&linkB);
213 busC.set_link(&linkC);
216 PJONAny &get_bus(
const uint8_t ix)
218 return ix == 0 ? busA : (ix == 1 ? busB : busC);
223 return linkA.strategy;
227 return linkB.strategy;
231 return linkC.strategy;