@@ -23,7 +23,8 @@ compile_error!(
23
23
) ;
24
24
25
25
use core:: cell:: RefCell ;
26
- use core:: ffi;
26
+ use core:: ffi:: { self , CStr } ;
27
+ use core:: fmt:: { self , Debug } ;
27
28
use core:: sync:: atomic:: * ;
28
29
29
30
use std:: fs;
@@ -38,7 +39,7 @@ use anyhow::{bail, Result};
38
39
39
40
use async_io:: { Async , Timer } ;
40
41
use esp_idf_svc:: http:: server:: { EspHttpConnection , Request } ;
41
- use esp_idf_svc:: io:: Write ;
42
+ use esp_idf_svc:: io:: { EspIOError , Write } ;
42
43
use log:: * ;
43
44
44
45
use esp_idf_svc:: sys:: EspError ;
@@ -584,7 +585,7 @@ fn test_tcp_bind() -> Result<()> {
584
585
585
586
fn test_timer (
586
587
eventloop : EspBackgroundEventLoop ,
587
- mut client : EspMqttClient < ConnState < MessageImpl , EspError > > ,
588
+ mut client : EspMqttClient < ' static > ,
588
589
) -> Result < EspTimer > {
589
590
info ! ( "About to schedule a one-shot timer for after 2 seconds" ) ;
590
591
let once_timer = EspTaskTimerService :: new ( ) ?. timer ( || {
@@ -602,7 +603,9 @@ fn test_timer(
602
603
603
604
let now = EspSystemTime { } . now ( ) ;
604
605
605
- eventloop. post ( & EventLoopMessage :: new ( now) , None ) . unwrap ( ) ;
606
+ eventloop
607
+ . post :: < CustomEvent > ( & CustomEvent :: new ( now) , delay:: NON_BLOCK )
608
+ . unwrap ( ) ;
606
609
607
610
client
608
611
. publish (
@@ -621,35 +624,39 @@ fn test_timer(
621
624
}
622
625
623
626
#[ derive( Copy , Clone , Debug ) ]
624
- struct EventLoopMessage ( Duration ) ;
627
+ struct CustomEvent ( Duration ) ;
625
628
626
- impl EventLoopMessage {
629
+ impl CustomEvent {
627
630
pub fn new ( duration : Duration ) -> Self {
628
631
Self ( duration)
629
632
}
630
633
}
631
634
632
- impl EspTypedEventSource for EventLoopMessage {
633
- fn source ( ) -> * const ffi:: c_char {
634
- b"DEMO-SERVICE\0 " . as_ptr ( ) as * const _
635
+ unsafe impl EspEventSource for CustomEvent {
636
+ fn source ( ) -> Option < & ' static CStr > {
637
+ // String should be unique across the whole project and ESP IDF
638
+ Some ( CStr :: from_bytes_with_nul ( b"DEMO-SERVICE\0 " ) . unwrap ( ) )
635
639
}
636
640
}
637
641
638
- impl EspTypedEventSerializer < EventLoopMessage > for EventLoopMessage {
639
- fn serialize < R > (
640
- event : & EventLoopMessage ,
641
- f : impl for < ' a > FnOnce ( & ' a EspEventPostData ) -> R ,
642
- ) -> R {
643
- f ( & unsafe { EspEventPostData :: new ( Self :: source ( ) , Self :: event_id ( ) , event) } )
642
+ impl EspEventSerializer for CustomEvent {
643
+ type Data < ' a > = CustomEvent ;
644
+
645
+ fn serialize < F , R > ( event : & Self :: Data < ' _ > , f : F ) -> R
646
+ where
647
+ F : FnOnce ( & EspEventPostData ) -> R ,
648
+ {
649
+ // Go the easy way since our payload implements Copy and is `'static`
650
+ f ( & unsafe { EspEventPostData :: new ( Self :: source ( ) . unwrap ( ) , Self :: event_id ( ) , event) } )
644
651
}
645
652
}
646
653
647
- impl EspTypedEventDeserializer < EventLoopMessage > for EventLoopMessage {
648
- fn deserialize < R > (
649
- data : & EspEventFetchData ,
650
- f : & mut impl for < ' a > FnMut ( & ' a EventLoopMessage ) -> R ,
651
- ) -> R {
652
- f ( unsafe { data. as_payload ( ) } )
654
+ impl EspEventDeserializer for CustomEvent {
655
+ type Data < ' a > = CustomEvent ;
656
+
657
+ fn deserialize < ' a > ( data : & EspEvent < ' a > ) -> Self :: Data < ' a > {
658
+ // Just as easy as serializing
659
+ * unsafe { data. as_payload :: < CustomEvent > ( ) }
653
660
}
654
661
}
655
662
@@ -658,14 +665,14 @@ fn test_eventloop() -> Result<(EspBackgroundEventLoop, EspBackgroundSubscription
658
665
let eventloop = EspBackgroundEventLoop :: new ( & Default :: default ( ) ) ?;
659
666
660
667
info ! ( "About to subscribe to the background event loop" ) ;
661
- let subscription = eventloop. subscribe ( |message : & EventLoopMessage | {
668
+ let subscription = eventloop. subscribe :: < CustomEvent , _ > ( |message| {
662
669
info ! ( "Got message from the event loop: {:?}" , message. 0 ) ;
663
670
} ) ?;
664
671
665
672
Ok ( ( eventloop, subscription) )
666
673
}
667
674
668
- fn test_mqtt_client ( ) -> Result < EspMqttClient < ' static , ConnState < MessageImpl , EspError > > > {
675
+ fn test_mqtt_client ( ) -> Result < EspMqttClient < ' static > > {
669
676
info ! ( "About to start MQTT client" ) ;
670
677
671
678
let conf = MqttClientConfiguration {
@@ -675,8 +682,7 @@ fn test_mqtt_client() -> Result<EspMqttClient<'static, ConnState<MessageImpl, Es
675
682
..Default :: default ( )
676
683
} ;
677
684
678
- let ( mut client, mut connection) =
679
- EspMqttClient :: new_with_conn ( "mqtts://broker.emqx.io:8883" , & conf) ?;
685
+ let ( mut client, mut connection) = EspMqttClient :: new ( "mqtts://broker.emqx.io:8883" , & conf) ?;
680
686
681
687
info ! ( "MQTT client started" ) ;
682
688
@@ -690,11 +696,8 @@ fn test_mqtt_client() -> Result<EspMqttClient<'static, ConnState<MessageImpl, Es
690
696
thread:: spawn ( move || {
691
697
info ! ( "MQTT Listening for messages" ) ;
692
698
693
- while let Some ( msg) = connection. next ( ) {
694
- match msg {
695
- Err ( e) => info ! ( "MQTT Message ERROR: {}" , e) ,
696
- Ok ( msg) => info ! ( "MQTT Message: {:?}" , msg) ,
697
- }
699
+ while let Ok ( event) = connection. next ( ) {
700
+ info ! ( "MQTT Event: {}" , event. payload( ) ) ;
698
701
}
699
702
700
703
info ! ( "MQTT connection loop exit" ) ;
@@ -832,7 +835,7 @@ fn test_https_client() -> anyhow::Result<()> {
832
835
let addr = "google.com:443" . to_socket_addrs ( ) ?. next ( ) . unwrap ( ) ;
833
836
let socket = Async :: < TcpStream > :: connect ( addr) . await ?;
834
837
835
- let mut tls = esp_idf_svc:: tls:: AsyncEspTls :: adopt ( EspTlsSocket :: new ( socket) ) ?;
838
+ let mut tls = esp_idf_svc:: tls:: EspAsyncTls :: adopt ( EspTlsSocket :: new ( socket) ) ?;
836
839
837
840
tls. negotiate ( "google.com" , & esp_idf_svc:: tls:: Config :: new ( ) )
838
841
. await ?;
@@ -1206,26 +1209,29 @@ fn httpd(
1206
1209
mutex : Arc < ( Mutex < Option < u32 > > , Condvar ) > ,
1207
1210
) -> Result < esp_idf_svc:: http:: server:: EspHttpServer < ' static > > {
1208
1211
use esp_idf_svc:: http:: server:: {
1209
- fn_handler, Connection , EspHttpServer , Handler , HandlerResult , Method , Middleware ,
1212
+ fn_handler, Connection , EspHttpServer , Handler , Method , Middleware ,
1210
1213
} ;
1211
1214
1212
1215
struct SampleMiddleware ;
1213
1216
1214
- impl < ' a > Middleware < EspHttpConnection < ' a > > for SampleMiddleware {
1215
- fn handle < H > ( & self , conn : & mut EspHttpConnection < ' a > , handler : & H ) -> HandlerResult
1216
- where
1217
- H : Handler < EspHttpConnection < ' a > > ,
1218
- {
1217
+ impl < ' a , H > Middleware < EspHttpConnection < ' a > , H > for SampleMiddleware
1218
+ where
1219
+ H : Handler < EspHttpConnection < ' a > > ,
1220
+ H :: Error : Debug + fmt:: Display + Send + Sync + ' static ,
1221
+ {
1222
+ type Error = anyhow:: Error ;
1223
+
1224
+ fn handle ( & self , conn : & mut EspHttpConnection < ' a > , handler : & H ) -> Result < ( ) , Self :: Error > {
1219
1225
info ! ( "Middleware called with uri: {}" , conn. uri( ) ) ;
1220
1226
1221
1227
if let Err ( err) = handler. handle ( conn) {
1222
1228
if !conn. is_response_initiated ( ) {
1223
1229
let mut resp = Request :: wrap ( conn) . into_status_response ( 500 ) ?;
1224
1230
1225
- write ! ( & mut resp, "ERROR: {err}" ) ?;
1231
+ write ! ( & mut resp, "ERROR: {err:? }" ) ?;
1226
1232
} else {
1227
1233
// Nothing can be done as the error happened after the response was initiated, propagate further
1228
- return Err ( err) ;
1234
+ Err ( anyhow :: Error :: msg ( err) ) ? ;
1229
1235
}
1230
1236
}
1231
1237
@@ -1235,11 +1241,13 @@ fn httpd(
1235
1241
1236
1242
struct SampleMiddleware2 ;
1237
1243
1238
- impl < ' a > Middleware < EspHttpConnection < ' a > > for SampleMiddleware2 {
1239
- fn handle < H > ( & self , conn : & mut EspHttpConnection < ' a > , handler : & H ) -> HandlerResult
1240
- where
1241
- H : Handler < EspHttpConnection < ' a > > ,
1242
- {
1244
+ impl < ' a , H > Middleware < EspHttpConnection < ' a > , H > for SampleMiddleware2
1245
+ where
1246
+ H : Handler < EspHttpConnection < ' a > > ,
1247
+ {
1248
+ type Error = H :: Error ;
1249
+
1250
+ fn handle ( & self , conn : & mut EspHttpConnection < ' a > , handler : & H ) -> Result < ( ) , H :: Error > {
1243
1251
info ! ( "Middleware2 called" ) ;
1244
1252
1245
1253
handler. handle ( conn)
@@ -1253,33 +1261,25 @@ fn httpd(
1253
1261
req. into_ok_response ( ) ?
1254
1262
. write_all ( "Hello from Rust!" . as_bytes ( ) ) ?;
1255
1263
1256
- Ok ( ( ) )
1257
- } ) ?
1258
- . fn_handler ( "/foo" , Method :: Get , |_| {
1259
- Result :: Err ( "Boo, something happened!" . into ( ) )
1264
+ Result :: < _ , EspIOError > :: Ok ( ( ) )
1260
1265
} ) ?
1266
+ . fn_handler ( "/foo" , Method :: Get , |_| bail ! ( "Boo, something happened!" ) ) ?
1261
1267
. fn_handler ( "/bar" , Method :: Get , |req| {
1262
1268
req. into_response ( 403 , Some ( "No permissions" ) , & [ ] ) ?
1263
- . write_all ( "You have no permissions to access this page" . as_bytes ( ) ) ?;
1264
-
1265
- Ok ( ( ) )
1269
+ . write_all ( "You have no permissions to access this page" . as_bytes ( ) )
1266
1270
} ) ?
1267
- . fn_handler ( "/panic" , Method :: Get , |_| panic ! ( "User requested a panic!" ) ) ?
1271
+ . fn_handler :: < ( ) , _ > ( "/panic" , Method :: Get , |_| panic ! ( "User requested a panic!" ) ) ?
1268
1272
. handler (
1269
1273
"/middleware" ,
1270
1274
Method :: Get ,
1271
- SampleMiddleware { } . compose ( fn_handler ( |_| {
1272
- Result :: Err ( "Boo, something happened!" . into ( ) )
1273
- } ) ) ,
1275
+ SampleMiddleware { } . compose ( fn_handler ( |_| bail ! ( "Boo, something happened!" ) ) ) ,
1274
1276
) ?
1275
1277
. handler (
1276
1278
"/middleware2" ,
1277
1279
Method :: Get ,
1278
1280
SampleMiddleware2 { } . compose ( SampleMiddleware { } . compose ( fn_handler ( |req| {
1279
1281
req. into_ok_response ( ) ?
1280
- . write_all ( "Middleware2 handler called" . as_bytes ( ) ) ?;
1281
-
1282
- Ok ( ( ) )
1282
+ . write_all ( "Middleware2 handler called" . as_bytes ( ) )
1283
1283
} ) ) ) ,
1284
1284
) ?;
1285
1285
@@ -1420,13 +1420,13 @@ fn wifi(
1420
1420
1421
1421
wifi. set_configuration ( & Configuration :: Mixed (
1422
1422
ClientConfiguration {
1423
- ssid : SSID . into ( ) ,
1424
- password : PASS . into ( ) ,
1423
+ ssid : SSID . try_into ( ) . unwrap ( ) ,
1424
+ password : PASS . try_into ( ) . unwrap ( ) ,
1425
1425
channel,
1426
1426
..Default :: default ( )
1427
1427
} ,
1428
1428
AccessPointConfiguration {
1429
- ssid : "aptest" . into ( ) ,
1429
+ ssid : "aptest" . try_into ( ) . unwrap ( ) ,
1430
1430
channel : channel. unwrap_or ( 1 ) ,
1431
1431
..Default :: default ( )
1432
1432
} ,
0 commit comments