@@ -24,7 +24,6 @@ use meshtastic::protobufs::{FromRadio, MeshPacket, PortNum};
2424use meshtastic:: types:: { MeshChannel , NodeId } ;
2525use meshtastic:: utils:: generate_rand_id;
2626use meshtastic:: utils:: stream:: { build_ble_stream, BleId } ;
27-
2827use meshtastic:: Message ;
2928
3029struct Router {
@@ -44,6 +43,7 @@ impl PacketRouter<(), Infallible> for Router {
4443 Ok ( ( ) )
4544 }
4645 fn handle_mesh_packet ( & mut self , packet : MeshPacket ) -> Result < ( ) , Infallible > {
46+ println ! ( "Mesh packet sent.." ) ;
4747 self . sent . push_back ( packet) ;
4848 Ok ( ( ) )
4949 }
@@ -53,18 +53,14 @@ impl PacketRouter<(), Infallible> for Router {
5353}
5454
5555enum RecievedPacket {
56- ConnectionClosed ,
5756 RoutingApp ( Data ) ,
5857 MyInfo ( MyNodeInfo ) ,
5958 NodeInfo ( NodeId , User ) ,
6059 Other ,
6160}
62- impl From < Option < FromRadio > > for RecievedPacket {
63- fn from ( from_radio : Option < FromRadio > ) -> Self {
61+ impl From < FromRadio > for RecievedPacket {
62+ fn from ( from_radio : FromRadio ) -> Self {
6463 use RecievedPacket :: * ;
65- let Some ( from_radio) = from_radio else {
66- return ConnectionClosed ;
67- } ;
6864 let Some ( payload) = from_radio. payload_variant else {
6965 return Other ;
7066 } ;
@@ -102,7 +98,7 @@ impl From<Option<FromRadio>> for RecievedPacket {
10298}
10399
104100async fn get_ble_device ( ) -> String {
105- println ! ( "Scanning devices 5s..." ) ;
101+ println ! ( "Scanning devices 5s, will connect if only one device is found, ..." ) ;
106102 let devices = meshtastic:: utils:: stream:: available_ble_devices ( Duration :: from_secs ( 5 ) )
107103 . await
108104 . expect ( "available_ble_devices failed" ) ;
@@ -153,7 +149,8 @@ async fn main() {
153149
154150 // Get MyInfo from the first message of stream
155151 // -----------------------------------------------------------------------
156- let RecievedPacket :: MyInfo ( my_node_info) = RecievedPacket :: from ( packet_rx. recv ( ) . await ) else {
152+ let from_radio = packet_rx. recv ( ) . await . expect ( "BLE stream closed" ) ;
153+ let RecievedPacket :: MyInfo ( my_node_info) = from_radio. into ( ) else {
157154 panic ! ( "Failed to receive MyInfo" ) ;
158155 } ;
159156
@@ -164,19 +161,18 @@ async fn main() {
164161 // ensuring there is room to send outgoing messages without issues.
165162 // -----------------------------------------------------------------------
166163
164+ // Map of node names to NodeId
167165 let mut nodes: BTreeMap < _ , _ > = [ ( String :: from ( "BROADCAST" ) , NodeId :: new ( u32:: MAX ) ) ] . into ( ) ;
168166
169167 print ! ( "Emptying I/O buffer & getting other nodes info..." ) ;
170168 loop {
171169 tokio:: select! {
172170 packet = packet_rx. recv( ) => {
171+ let packet = packet. expect( "BLE stream closed" ) ;
173172 match RecievedPacket :: from( packet) . into( ) {
174173 RecievedPacket :: NodeInfo ( node_id, node_info) => {
175174 nodes. insert( node_info. short_name, node_id) ;
176175 }
177- RecievedPacket :: ConnectionClosed => {
178- panic!( "Connection closed" ) ;
179- }
180176 _ => { }
181177 }
182178 print!( "." ) ;
@@ -190,7 +186,7 @@ async fn main() {
190186
191187 let Some ( to) = nodes. get ( & to) else {
192188 println ! ( "\n Available nodes: {:?}" , nodes. keys( ) ) ;
193- panic ! ( "Specified node not found" ) ;
189+ panic ! ( "Specified node '{to}' not found" ) ;
194190 } ;
195191
196192 // Send a message
@@ -211,25 +207,23 @@ async fn main() {
211207
212208 let sent_packet = packet_router. sent . pop_front ( ) . unwrap ( ) ;
213209
214- println ! ( " sent." ) ;
215-
216210 // Wait for ACK
217211 // -----------------------------------------------------------------------
218212 print ! ( "Waiting for ACK (packet_id={})..." , sent_packet. id) ;
219213 std:: io:: stdout ( ) . flush ( ) . unwrap ( ) ;
220214
221215 loop {
222- match packet_rx. recv ( ) . await . into ( ) {
216+ let from_radio = packet_rx. recv ( ) . await . expect ( "BLE stream closed" ) ;
217+ match from_radio. into ( ) {
223218 RecievedPacket :: RoutingApp ( data) => {
224219 if data. portnum == PortNum :: RoutingApp as i32 && data. request_id == sent_packet. id {
225220 println ! ( "got ACK" ) ;
226221 break ;
227222 }
228223 }
229- RecievedPacket :: ConnectionClosed => {
230- panic ! ( "Connection closed" ) ;
231- }
232224 _ => { }
233225 }
234226 }
227+
228+ let _ = stream_api. disconnect ( ) . await . expect ( "Unable to disconnect" ) ;
235229}
0 commit comments