#include "Interface.h" #include "Identity.h" #include "Transport.h" using namespace RNS; using namespace RNS::Type::Interface; /*static*/ uint8_t Interface::DISCOVER_PATHS_FOR = MODE_ACCESS_POINT | MODE_GATEWAY; void InterfaceImpl::handle_outgoing(const Bytes& data) { //TRACEF("InterfaceImpl.handle_outgoing: data: %s", data.toHex().c_str()); TRACE("InterfaceImpl.handle_outgoing"); _txb += data.size(); } void InterfaceImpl::handle_incoming(const Bytes& data) { //TRACEF("InterfaceImpl.handle_incoming: data: %s", data.toHex().c_str()); TRACE("InterfaceImpl.handle_incoming"); _rxb += data.size(); // Create temporary Interface encapsulating our own shared impl std::shared_ptr self = shared_from_this(); Interface interface(self); // Pass data on to transport for handling Transport::inbound(data, interface); } void Interface::send_outgoing(const Bytes& data) { assert(_impl); //TRACEF("Interface.send_outgoing: data: %s", data.toHex().c_str()); //TRACE("Interface.send_outgoing"); // Catch exceptions from calls into Interface implementation try { _impl->send_outgoing(data); } catch (const std::bad_alloc&) { ERROR("Interface::send_outgoing: bad_alloc - OUT OF MEMORY"); // Critical OOM, restarting #if defined(ESP32) ESP.restart(); #elif defined(ARDUINO_ARCH_NRF52) || defined(ARDUINO_NRF52_ADAFRUIT) NVIC_SystemReset(); #endif } catch (std::exception& e) { ERRORF("Interface::send_outgoing: %s", e.what()); } } void Interface::handle_incoming(const Bytes& data) { assert(_impl); //TRACEF("Interface.handle_incoming: data: %s", data.toHex().c_str()); //TRACE("Interface.handle_incoming"); /* _impl->_rxb += data.size(); // Pass data on to transport for handling Transport::inbound(data, *this); */ // Catch exceptions from calls into Interface implementation try { _impl->handle_incoming(data); } catch (const std::bad_alloc&) { ERROR("Interface::handle_incoming: bad_alloc - OUT OF MEMORY"); // Critical OOM, restarting #if defined(ESP32) ESP.restart(); #elif defined(ARDUINO_ARCH_NRF52) || defined(ARDUINO_NRF52_ADAFRUIT) NVIC_SystemReset(); #endif } catch (std::exception& e) { ERRORF("Interface::handle_incoming: %s", e.what()); } } void Interface::process_announce_queue() { /* if not hasattr(self, "announce_cap"): self.announce_cap = RNS.Reticulum.ANNOUNCE_CAP if hasattr(self, "announce_queue"): try: now = time.time() stale = [] for a in self.announce_queue: if now > a["time"]+RNS.Reticulum.QUEUED_ANNOUNCE_LIFE: stale.append(a) for s in stale: if s in self.announce_queue: self.announce_queue.remove(s) if len(self.announce_queue) > 0: min_hops = min(entry["hops"] for entry in self.announce_queue) entries = list(filter(lambda e: e["hops"] == min_hops, self.announce_queue)) entries.sort(key=lambda e: e["time"]) selected = entries[0] double now = OS::time(); uint32_t wait_time = 0; if (_impl->_bitrate > 0 && _impl->_announce_cap > 0) { uint32_t tx_time = (len(selected["raw"])*8) / _impl->_bitrate; wait_time = (tx_time / _impl->_announce_cap); } _impl->_announce_allowed_at = now + wait_time; self.on_outgoing(selected["raw"]) if selected in self.announce_queue: self.announce_queue.remove(selected) if len(self.announce_queue) > 0: timer = threading.Timer(wait_time, self.process_announce_queue) timer.start() except Exception as e: self.announce_queue = [] RNS.log("Error while processing announce queue on "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR) RNS.log("The announce queue for this interface has been cleared.", RNS.LOG_ERROR) */ } /* void ArduinoJson::convertFromJson(JsonVariantConst src, RNS::Interface& dst) { TRACE(">>> Deserializing Interface"); TRACEF(">>> Interface pre: %s", dst.debugString().c_str()); if (!src.isNull()) { RNS::Bytes hash; hash.assignHex(src.as()); TRACEF(">>> Querying Transport for Interface hash %s", hash.toHex().c_str()); // Query transport for matching interface dst = Transport::find_interface_from_hash(hash); TRACEF(">>> Interface post: %s", dst.debugString().c_str()); } else { dst = {RNS::Type::NONE}; TRACEF(">>> Interface post: %s", dst.debugString().c_str()); } } */