diff --git a/exercises/204_established_identities/src/main.cpp b/exercises/204_established_identities/src/main.cpp index 4ca6ec1..dab683d 100644 --- a/exercises/204_established_identities/src/main.cpp +++ b/exercises/204_established_identities/src/main.cpp @@ -703,6 +703,14 @@ static String parse_recipient_label(const String& text) { } static void attach_link_callbacks(RNS::Link& link) { + if (link) { + Serial.printf("APP LINK CALLBACKS: attach hash=%s status=%u initiator=%u\r\n", + link.hash().toHex().c_str(), + (unsigned)link.status(), + link.initiator() ? 1U : 0U); + } else { + Serial.println("APP LINK CALLBACKS: attach skipped null link"); + } link.set_packet_callback(on_link_packet); link.set_link_closed_callback(on_link_closed); } @@ -716,6 +724,14 @@ static void on_link_packet(const RNS::Bytes& data, const RNS::Packet& packet) { String text = data.toString().c_str(); String sender = parse_sender_label(text); String recipient = parse_recipient_label(text); + Serial.printf("APP RX LINK ENTER: link=%s status=%u initiator=%u peer_index=%d sender=%s recipient=%s text=%s\r\n", + packet.link() ? packet.link().hash().toHex().c_str() : "(none)", + packet.link() ? (unsigned)packet.link().status() : 255U, + packet.link() && packet.link().initiator() ? 1U : 0U, + peer_index, + sender.c_str(), + recipient.c_str(), + text.c_str()); if (sender == NODE_LABEL || (recipient.length() > 0 && recipient != NODE_LABEL)) { Serial.printf("RX LINK ignored: self_or_wrong_recipient text=%s\r\n", text.c_str()); return; @@ -795,6 +811,11 @@ static void on_link_closed(RNS::Link& link) { static void on_outbound_link_established(RNS::Link& link) { int peer_index = find_peer_by_link_hash(link.hash()); + Serial.printf("APP LINK ESTABLISHED CB: direction=outbound hash=%s status=%u initiator=%u peer_index=%d\r\n", + link.hash().toHex().c_str(), + (unsigned)link.status(), + link.initiator() ? 1U : 0U, + peer_index); if (peer_index >= 0) { peers[peer_index].outbound_link = link; attach_link_callbacks(peers[peer_index].outbound_link); @@ -817,6 +838,11 @@ static void on_inbound_link_established(RNS::Link& link) { if (peer_index < 0) { peer_index = first_free_peer_slot(); } + Serial.printf("APP LINK ESTABLISHED CB: direction=inbound hash=%s status=%u initiator=%u peer_index=%d\r\n", + link.hash().toHex().c_str(), + (unsigned)link.status(), + link.initiator() ? 1U : 0U, + peer_index); if (peer_index >= 0) { peers[peer_index].inbound_link = link; attach_link_callbacks(peers[peer_index].inbound_link); @@ -942,6 +968,12 @@ static void maybe_open_link(const DateTime& rtc_now, bool have_rtc_now) { continue; } if (peer.outbound_active || (peer.outbound_link && peer.outbound_link.status() == RNS::Type::Link::ACTIVE)) { + if (!peer.outbound_active) { + Serial.printf("APP LINK STATE: outbound already active peer=%s hash=%s status=%u\r\n", + peer.label.c_str(), + peer.outbound_link.hash().toHex().c_str(), + (unsigned)peer.outbound_link.status()); + } peer.outbound_active = true; continue; } @@ -1108,13 +1140,27 @@ void loop() { link = &peer.inbound_link; } if (!link) { + Serial.printf("TX LINK SKIP: peer=%s outbound=%u outbound_status=%u inbound=%u inbound_status=%u attempted=%u active_out=%u active_in=%u\r\n", + peer.label.c_str(), + peer.outbound_link ? 1U : 0U, + peer.outbound_link ? (unsigned)peer.outbound_link.status() : 255U, + peer.inbound_link ? 1U : 0U, + peer.inbound_link ? (unsigned)peer.inbound_link.status() : 255U, + peer.outbound_attempted ? 1U : 0U, + peer.outbound_active ? 1U : 0U, + peer.inbound_active ? 1U : 0U); continue; } peer.last_tx_second = rtc_now.second; peer.last_tx_ms = now; String message = String("Hi from ") + NODE_LABEL + " iter=" + String(peer.tx_iter++) + " to=" + peer.label; - Serial.printf("TX LINK: %s\r\n", message.c_str()); + Serial.printf("TX LINK: %s via=%s hash=%s status=%u since_open=%u\r\n", + message.c_str(), + link == &peer.outbound_link ? "outbound" : "inbound", + link->hash().toHex().c_str(), + (unsigned)link->status(), + (unsigned)peer.tx_since_link_open); show_status("TX LINK", peer.label.c_str(), message.c_str()); RNS::Packet(*link, RNS::bytesFromString(message.c_str())).send(); ++peer.tx_since_link_open;