|
| 1 | +/* This CompositeLink class transfers PJON packets over one or more Links, having a routing table that specifies which link |
| 2 | + must be used to reach a specific device. Devices which have not been registered will be reached through the default link. |
| 3 | + |
| 4 | + A concrete use for this class can be where most devices can be reached through a wire, but some must be reached through |
| 5 | + a wireless connection using. And perhaps some others that must be reached through Ethernet (TCP or UDP). |
| 6 | +*/ |
| 7 | + |
| 8 | +#pragma once |
| 9 | + |
| 10 | +#include <PJONLink.h> |
| 11 | + |
| 12 | +// This define can be set to increase or decrease the max number of registered routes |
| 13 | +#ifndef MAX_LINK_ROUTES |
| 14 | +#define MAX_LINK_ROUTES 10 |
| 15 | +#endif |
| 16 | + |
| 17 | +#define MAX_LINKS 5 |
| 18 | + |
| 19 | +class CompositeLink : public Link { |
| 20 | + Link *default_link = NULL; |
| 21 | + int8_t num_links = 0; |
| 22 | + Link *links[MAX_LINKS]; // A collection of all registered links in addition to the default |
| 23 | + |
| 24 | + uint8_t num_routes = 0; |
| 25 | + uint8_t device_ids[MAX_LINK_ROUTES]; // Each device's id... |
| 26 | + uint8_t link_ix[MAX_LINK_ROUTES]; // ...and the index in the links array of the link through which it can be reached |
| 27 | + |
| 28 | + PacketInfo lpi; |
| 29 | + long dummy_bus_id = 0; |
| 30 | + |
| 31 | + // Return array position of link if already registered, or -1 if not |
| 32 | + int8_t find_link(const Link* link) { |
| 33 | + for (int8_t i=0; i<num_links; i++) if (links[i] == link) return i; |
| 34 | + return -1; |
| 35 | + } |
| 36 | + |
| 37 | + // Find the link to be used for reaching a device |
| 38 | + Link *find_link_by_device(uint8_t device_id) { |
| 39 | + for (int8_t i=0; i<num_links; i++) if (device_ids[i] == device_id) return links[link_ix[i]]; |
| 40 | + return default_link; |
| 41 | + } |
| 42 | + |
| 43 | + // Locate link or add it, then return is position in the array, -1 if not added. |
| 44 | + int8_t register_link(Link *link) { |
| 45 | + int8_t ix = find_link(link); |
| 46 | + if (ix < 0 && num_links < MAX_LINKS) { // Not present, must be added |
| 47 | + links[num_links++] = link; |
| 48 | + link->set_receiver(link_receiver_callback, this); |
| 49 | + return num_links-1; |
| 50 | + } |
| 51 | + return ix; |
| 52 | + } |
| 53 | + |
| 54 | + static void link_receiver_callback(uint8_t id, const uint8_t *payload, uint16_t length, void *callback_object) { |
| 55 | + if (callback_object && ((CompositeLink*)callback_object)->_receiver) |
| 56 | + ((CompositeLink*)callback_object)->_receiver((uint8_t*)payload, length, ((CompositeLink*)callback_object)->lpi); |
| 57 | + } |
| 58 | + receiver _receiver = NULL; |
| 59 | + |
| 60 | +public: |
| 61 | + |
| 62 | + CompositeLink() {} |
| 63 | + |
| 64 | + // All devices not having an explicit association to a link will be reached through the default link. |
| 65 | + // Note that the default link MUST BE SET. |
| 66 | + void set_default_route(Link *link) { default_link = link; default_link.set_receiver(link_receiver_callback, this); } |
| 67 | + |
| 68 | + // Register that a specific device must be reached through a specific link |
| 69 | + bool add_route(uint8_t device_id, Link *link) { |
| 70 | + if (num_devices >= MAX_LINK_ROUTES) return false; |
| 71 | + device_ids[num_devices] = device_id; |
| 72 | + link_ix[num_devices] = register_link(link); |
| 73 | + if (link_ix[num_devices] < 0) return false; // Could not add link -- too many? |
| 74 | + num_devices++; |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + //**************** Overridden functions below ******************* |
| 80 | + |
| 81 | + uint16_t receive() { |
| 82 | + for (uint8_t i=0; i<num_links; i++) { |
| 83 | + uint16_t v = links[i].receive(); |
| 84 | + if (v == ACK) return v; |
| 85 | + } |
| 86 | + return default_link->receive(); |
| 87 | + } |
| 88 | + |
| 89 | + uint16_t receive(uint32_t duration) { |
| 90 | + uint16_t response; |
| 91 | + uint32_t time = micros(); |
| 92 | + while((uint32_t)(micros() - time) <= duration) { |
| 93 | + response = receive(); |
| 94 | + if(response == ACK) return ACK; |
| 95 | + } |
| 96 | + return response; |
| 97 | + }; |
| 98 | + |
| 99 | + uint8_t update() { |
| 100 | + uint8_t num_packets = 0; |
| 101 | + num_packets += default_link->update(); |
| 102 | + for (uint8_t i=0; i<num_links; i++) num_packets += links[i]->update(); |
| 103 | + return num_packets; |
| 104 | + } |
| 105 | + |
| 106 | + uint16_t send_packet(uint8_t id, const uint8_t *b_id, const char *string, uint16_t length, uint32_t duration, uint8_t header) { |
| 107 | + // Remember last packet info |
| 108 | + memset(&lpi, 0, sizeof lpi); |
| 109 | + lpi.header = header==0 ? SENDER_INFO_BIT | ACK_REQUEST_BIT | MI_PJON_BIT | MI_EXTENDED_HEADER_BIT : header; |
| 110 | + lpi.receiver_id = id; |
| 111 | + copy_bus_id(lpi.receiver_bus_id, b_id); |
| 112 | + lpi.sender_id = get_id(); |
| 113 | + copy_bus_id(lpi.sender_bus_id, (uint8_t*) &dummy_bus_id); |
| 114 | + |
| 115 | + // Find link and ask it to send |
| 116 | + Link *link = find_link_by_device(id); |
| 117 | + return link->send_packet(id, b_id, string, length, duration, header); |
| 118 | + } |
| 119 | + |
| 120 | + const PacketInfo &get_last_packet_info() const { return lpi; } |
| 121 | + uint16_t get_header() const { return lpi.header; } |
| 122 | + |
| 123 | + uint8_t get_id() const { return link.get_id(); } |
| 124 | + const uint8_t *get_bus_id() const { return (const uint8_t*) &dummy_bus_id; } |
| 125 | + |
| 126 | + void set_receiver(receiver r) { _receiver = r; } |
| 127 | +}; |
| 128 | + |
0 commit comments