1414use std:: collections:: { BTreeMap , VecDeque } ;
1515use std:: convert:: Infallible ;
1616use std:: io:: Write ;
17- use std:: time:: Duration ;
17+ use std:: time:: { Duration , Instant } ;
1818
1919use meshtastic:: api:: StreamApi ;
2020use meshtastic:: packet:: { PacketDestination , PacketRouter } ;
@@ -25,11 +25,11 @@ use meshtastic::types::{MeshChannel, NodeId};
2525use meshtastic:: utils:: generate_rand_id;
2626use meshtastic:: utils:: stream:: { build_ble_stream, BleId } ;
2727use meshtastic:: Message ;
28-
2928struct Router {
3029 sent : VecDeque < MeshPacket > ,
3130 node_id : NodeId ,
3231}
32+
3333impl Router {
3434 fn new ( node_id : NodeId ) -> Self {
3535 Self {
@@ -38,6 +38,7 @@ impl Router {
3838 }
3939 }
4040}
41+
4142impl PacketRouter < ( ) , Infallible > for Router {
4243 fn handle_packet_from_radio ( & mut self , _packet : FromRadio ) -> Result < ( ) , Infallible > {
4344 Ok ( ( ) )
@@ -53,11 +54,12 @@ impl PacketRouter<(), Infallible> for Router {
5354}
5455
5556enum RecievedPacket {
56- RoutingApp ( Data ) ,
57+ RoutingApp ( MeshPacket , Data ) ,
5758 MyInfo ( MyNodeInfo ) ,
5859 NodeInfo ( NodeId , User ) ,
5960 Other ,
6061}
62+
6163impl From < FromRadio > for RecievedPacket {
6264 fn from ( from_radio : FromRadio ) -> Self {
6365 use RecievedPacket :: * ;
@@ -74,14 +76,14 @@ impl From<FromRadio> for RecievedPacket {
7476 }
7577 }
7678 PayloadVariant :: Packet ( recv_packet) => {
77- let Some ( pv) = recv_packet. payload_variant else {
79+ let Some ( pv) = recv_packet. payload_variant . clone ( ) else {
7880 return Other ;
7981 } ;
8082 let mesh_packet:: PayloadVariant :: Decoded ( data) = pv else {
8183 return Other ;
8284 } ;
8385 match PortNum :: try_from ( data. portnum ) {
84- Ok ( PortNum :: RoutingApp ) => RoutingApp ( data) ,
86+ Ok ( PortNum :: RoutingApp ) => RoutingApp ( recv_packet , data) ,
8587 Ok ( PortNum :: NodeinfoApp ) => {
8688 if let Ok ( user) = User :: decode ( data. payload . as_slice ( ) ) {
8789 NodeInfo ( NodeId :: new ( recv_packet. from ) , user)
@@ -167,18 +169,16 @@ async fn main() {
167169 print ! ( "Emptying I/O buffer & getting other nodes info..." ) ;
168170 loop {
169171 tokio:: select! {
170- packet = packet_rx. recv( ) => {
171- let packet = packet. expect( "BLE stream closed" ) ;
172- match RecievedPacket :: from( packet) . into( ) {
173- RecievedPacket :: NodeInfo ( node_id, node_info) => {
174- nodes. insert( node_info. short_name, node_id) ;
175- }
176- _ => { }
177- }
172+ from_radio = packet_rx. recv( ) => {
178173 print!( "." ) ;
174+ let from_radio = from_radio. expect( "BLE stream closed" ) ;
175+ let RecievedPacket :: NodeInfo ( node_id, node_info) = RecievedPacket :: from( from_radio) . into( ) else {
176+ continue ;
177+ } ;
178+ nodes. insert( node_info. short_name, node_id) ;
179179 std:: io:: stdout( ) . flush( ) . unwrap( ) ;
180180 }
181- _ = tokio:: time:: sleep( Duration :: from_millis( 200 ) ) => {
181+ _ = tokio:: time:: sleep( Duration :: from_millis( 1000 ) ) => {
182182 break ;
183183 }
184184 }
@@ -189,6 +189,8 @@ async fn main() {
189189 panic ! ( "Specified node '{to}' not found" ) ;
190190 } ;
191191
192+ println ! ( "Destination node {}" , to. id( ) ) ;
193+
192194 // Send a message
193195 // -----------------------------------------------------------------------
194196 print ! ( "\n Sending message..." ) ;
@@ -212,18 +214,35 @@ async fn main() {
212214 print ! ( "Waiting for ACK (packet_id={})..." , sent_packet. id) ;
213215 std:: io:: stdout ( ) . flush ( ) . unwrap ( ) ;
214216
215- loop {
216- let from_radio = packet_rx. recv ( ) . await . expect ( "BLE stream closed" ) ;
217- match from_radio. into ( ) {
218- RecievedPacket :: RoutingApp ( data) => {
219- if data. portnum == PortNum :: RoutingApp as i32 && data. request_id == sent_packet. id {
220- println ! ( "got ACK" ) ;
221- break ;
217+ let start = Instant :: now ( ) ;
218+ let mut found = false ;
219+ while start. elapsed ( ) . as_secs ( ) < 60 && !found {
220+ print ! ( "." ) ;
221+ std:: io:: stdout ( ) . flush ( ) . unwrap ( ) ;
222+ tokio:: select! {
223+ from_radio = packet_rx. recv( ) => {
224+ let from_radio = from_radio. expect( "BLE stream closed" ) ;
225+ let RecievedPacket :: RoutingApp ( mesh_packet, data) = RecievedPacket :: from( from_radio) . into( ) else {
226+ continue ;
227+ } ;
228+ if data. portnum == PortNum :: RoutingApp as i32
229+ && data. request_id == sent_packet. id
230+ && mesh_packet. from == to. id( )
231+ && mesh_packet. to == my_node_info. my_node_num
232+ {
233+ found = true ;
222234 }
223235 }
224- _ => { }
236+ _ = tokio:: time:: sleep( Duration :: from_millis( 1000 ) ) => {
237+ }
225238 }
226239 }
227240
228- let _ = stream_api. disconnect ( ) . await . expect ( "Unable to disconnect" ) ;
241+ if found {
242+ println ! ( "got ACK in {}s" , start. elapsed( ) . as_secs( ) ) ;
243+ } else {
244+ println ! ( "ACK timeout" ) ;
245+ }
246+
247+ let _ = stream_api. disconnect ( ) . await ;
229248}
0 commit comments