@@ -30,38 +30,40 @@ use core::pin::pin;
30
30
31
31
use std:: net:: UdpSocket ;
32
32
33
- use comm:: WifiNwCommCluster ;
34
33
use embassy_futures:: select:: { select, select4} ;
35
34
36
35
use embassy_sync:: blocking_mutex:: raw:: NoopRawMutex ;
37
36
use embassy_time:: { Duration , Timer } ;
38
- use log:: { info, warn } ;
37
+ use log:: info;
39
38
40
39
use rs_matter:: core:: Matter ;
41
40
use rs_matter:: data_model:: device_types:: DEV_TYPE_ON_OFF_LIGHT ;
42
- use rs_matter:: data_model:: objects:: * ;
41
+ use rs_matter:: data_model:: networks:: unix:: UnixNetifs ;
42
+ use rs_matter:: data_model:: networks:: wireless:: {
43
+ NetCtlState , NetCtlWithStatusImpl , NoopWirelessNetCtl , WifiNetworks ,
44
+ } ;
45
+ use rs_matter:: data_model:: objects:: {
46
+ Async , AsyncHandler , AsyncMetadata , Dataver , EmptyHandler , Endpoint , Node ,
47
+ } ;
43
48
use rs_matter:: data_model:: on_off:: { self , ClusterHandler as _} ;
44
49
use rs_matter:: data_model:: root_endpoint;
45
- use rs_matter:: data_model:: sdm:: wifi_nw_diagnostics:: {
46
- self , WiFiSecurity , WiFiVersion , WifiNwDiagCluster , WifiNwDiagData ,
47
- } ;
50
+ use rs_matter:: data_model:: sdm:: net_comm:: { NetCtl , NetCtlStatus , NetworkType , Networks } ;
51
+ use rs_matter:: data_model:: sdm:: wifi_diag:: WifiDiag ;
48
52
use rs_matter:: data_model:: subscriptions:: Subscriptions ;
49
- use rs_matter:: data_model:: system_model:: descriptor ;
53
+ use rs_matter:: data_model:: system_model:: desc :: { self , ClusterHandler as _ } ;
50
54
use rs_matter:: error:: Error ;
51
55
use rs_matter:: mdns:: MdnsService ;
52
56
use rs_matter:: pairing:: DiscoveryCapabilities ;
53
57
use rs_matter:: persist:: Psm ;
54
58
use rs_matter:: respond:: DefaultResponder ;
55
- use rs_matter:: test_device;
56
59
use rs_matter:: transport:: core:: MATTER_SOCKET_BIND_ADDR ;
57
60
use rs_matter:: transport:: network:: btp:: { Btp , BtpContext } ;
58
61
use rs_matter:: utils:: select:: Coalesce ;
59
62
use rs_matter:: utils:: storage:: pooled:: PooledBuffers ;
60
- use rs_matter:: utils:: sync:: { blocking:: raw:: StdRawMutex , Notification } ;
61
- use rs_matter:: MATTER_PORT ;
63
+ use rs_matter:: utils:: sync:: blocking:: raw:: StdRawMutex ;
64
+ use rs_matter:: { clusters, test_device} ;
65
+ use rs_matter:: { devices, MATTER_PORT } ;
62
66
63
- #[ path = "../common/comm.rs" ]
64
- mod comm;
65
67
#[ path = "../common/mdns.rs" ]
66
68
mod mdns;
67
69
@@ -94,11 +96,17 @@ fn main() -> Result<(), Error> {
94
96
// Our on-off cluster
95
97
let on_off = on_off:: OnOffHandler :: new ( Dataver :: new_rand ( matter. rand ( ) ) ) ;
96
98
97
- // A notification when the Wifi is setup, so that Matter over UDP can start
98
- let wifi_complete = Notification :: new ( ) ;
99
+ // A storage for the Wifi networks
100
+ let networks = WifiNetworks :: < 3 , NoopRawMutex > :: new ( ) ;
101
+
102
+ // The network controller
103
+ // We would be using a "fake" network controller that does not actually manage any Wifi networks
104
+ let net_ctl_state = NetCtlState :: new_with_mutex :: < NoopRawMutex > ( ) ;
105
+ let net_ctl =
106
+ NetCtlWithStatusImpl :: new ( & net_ctl_state, NoopWirelessNetCtl :: new ( NetworkType :: Wifi ) ) ;
99
107
100
108
// Assemble our Data Model handler by composing the predefined Root Endpoint handler with the On/Off handler
101
- let dm_handler = Async ( dm_handler ( & matter, & on_off, & wifi_complete ) ) ;
109
+ let dm_handler = dm_handler ( & matter, & on_off, & net_ctl , & networks ) ;
102
110
103
111
// Create a default responder capable of handling up to 3 subscriptions
104
112
// All other subscription requests will be turned down with "resource exhausted"
@@ -127,8 +135,9 @@ fn main() -> Result<(), Error> {
127
135
let dir = std:: env:: temp_dir ( ) . join ( "rs-matter" ) ;
128
136
129
137
psm. load ( & dir, & matter) ?;
138
+ psm. load_networks ( & dir, & networks) ?;
130
139
131
- let mut persist = pin ! ( psm. run ( dir, & matter) ) ;
140
+ let mut persist = pin ! ( psm. run_with_networks ( dir, & matter, Some ( & networks ) ) ) ;
132
141
133
142
// Create and run the mDNS responder
134
143
let mut mdns = pin ! ( mdns:: run_mdns( & matter) ) ;
@@ -145,23 +154,16 @@ fn main() -> Result<(), Error> {
145
154
) ) ;
146
155
147
156
let mut transport = pin ! ( matter. run( & btp, & btp, DiscoveryCapabilities :: BLE ) ) ;
148
-
149
- let mut wifi_complete_task = pin ! ( async {
150
- wifi_complete. wait( ) . await ;
151
- warn!(
152
- "Wifi setup complete, giving 4 seconds to BTP to finish any outstanding messages"
153
- ) ;
154
-
155
- Timer :: after( Duration :: from_secs( 4 ) ) . await ;
156
-
157
+ let mut wifi_prov_task = pin ! ( async {
158
+ NetCtlState :: wait_prov_ready( & net_ctl_state, & btp) . await ;
157
159
Ok ( ( ) )
158
160
} ) ;
159
161
160
162
// Combine all async tasks in a single one
161
163
let all = select4 (
162
164
& mut transport,
163
165
& mut bluetooth,
164
- select ( & mut wifi_complete_task , & mut persist) . coalesce ( ) ,
166
+ select ( & mut wifi_prov_task , & mut persist) . coalesce ( ) ,
165
167
select ( & mut respond, & mut device) . coalesce ( ) ,
166
168
) ;
167
169
@@ -193,55 +195,49 @@ fn main() -> Result<(), Error> {
193
195
const NODE : Node < ' static > = Node {
194
196
id : 0 ,
195
197
endpoints : & [
196
- root_endpoint:: endpoint ( 0 , root_endpoint:: OperNwType :: Wifi ) ,
198
+ root_endpoint:: root_endpoint ( NetworkType :: Wifi ) ,
197
199
Endpoint {
198
200
id : 1 ,
199
- device_types : & [ DEV_TYPE_ON_OFF_LIGHT ] ,
200
- clusters : & [ descriptor :: CLUSTER , on_off:: OnOffHandler :: CLUSTER ] ,
201
+ device_types : devices ! ( DEV_TYPE_ON_OFF_LIGHT ) ,
202
+ clusters : clusters ! ( desc :: DescHandler :: CLUSTER , on_off:: OnOffHandler :: CLUSTER ) ,
201
203
} ,
202
204
] ,
203
205
} ;
204
206
205
207
/// The Data Model handler + meta-data for our Matter device.
206
208
/// The handler is the root endpoint 0 handler plus the on-off handler and its descriptor.
207
- fn dm_handler < ' a > (
209
+ fn dm_handler < ' a , N > (
208
210
matter : & ' a Matter < ' a > ,
209
211
on_off : & ' a on_off:: OnOffHandler ,
210
- nw_setup_complete : & ' a Notification < NoopRawMutex > ,
211
- ) -> impl Metadata + NonBlockingHandler + ' a {
212
+ net_ctl : & ' a N ,
213
+ networks : & ' a dyn Networks ,
214
+ ) -> impl AsyncMetadata + AsyncHandler + ' a
215
+ where
216
+ N : NetCtl + NetCtlStatus + WifiDiag ,
217
+ {
212
218
(
213
219
NODE ,
214
- root_endpoint:: handler (
215
- 0 ,
216
- Async ( WifiNwCommCluster :: new (
217
- Dataver :: new_rand ( matter. rand ( ) ) ,
218
- nw_setup_complete,
219
- ) ) ,
220
- wifi_nw_diagnostics:: ID ,
221
- Async ( WifiNwDiagCluster :: new (
222
- Dataver :: new_rand ( matter. rand ( ) ) ,
223
- WifiNwDiagData {
224
- bssid : [ 0 ; 6 ] ,
225
- security_type : WiFiSecurity :: Wpa2Personal ,
226
- wifi_version : WiFiVersion :: B ,
227
- channel_number : 20 ,
228
- rssi : 0 ,
229
- } ,
230
- ) ) ,
231
- & false ,
220
+ root_endpoint:: with_wifi (
221
+ & ( ) ,
222
+ & UnixNetifs ,
223
+ net_ctl,
224
+ networks,
232
225
matter. rand ( ) ,
233
- )
234
- . chain (
235
- 1 ,
236
- descriptor:: ID ,
237
- Async ( descriptor:: DescriptorCluster :: new ( Dataver :: new_rand (
226
+ root_endpoint:: with_sys (
227
+ & false ,
238
228
matter. rand ( ) ,
239
- ) ) ) ,
240
- )
241
- . chain (
242
- 1 ,
243
- on_off:: OnOffHandler :: CLUSTER . id ,
244
- Async ( on_off:: HandlerAdaptor ( on_off) ) ,
229
+ EmptyHandler
230
+ . chain (
231
+ 1 ,
232
+ desc:: DescHandler :: CLUSTER . id ,
233
+ Async ( desc:: DescHandler :: new ( Dataver :: new_rand ( matter. rand ( ) ) ) . adapt ( ) ) ,
234
+ )
235
+ . chain (
236
+ 1 ,
237
+ on_off:: OnOffHandler :: CLUSTER . id ,
238
+ Async ( on_off:: HandlerAdaptor ( on_off) ) ,
239
+ ) ,
240
+ ) ,
245
241
) ,
246
242
)
247
243
}
0 commit comments