15
15
* limitations under the License.
16
16
*/
17
17
18
+ use strum:: FromRepr ;
19
+
18
20
use crate :: {
21
+ attribute_enum,
19
22
data_model:: objects:: {
20
- AttrDataEncoder , AttrDetails , ChangeNotifier , Cluster , Dataver , Handler ,
21
- NonBlockingHandler , ATTRIBUTE_LIST , FEATURE_MAP ,
23
+ Access , AttrDataEncoder , AttrDataWriter , AttrDetails , AttrType , Attribute , ChangeNotifier ,
24
+ Cluster , Dataver , Handler , NonBlockingHandler , Quality , ATTRIBUTE_LIST , FEATURE_MAP ,
22
25
} ,
23
- error:: { Error , ErrorCode } ,
26
+ error:: Error ,
27
+ tlv:: { OctetStr , TLVWriter , TagType , ToTLV } ,
24
28
utils:: rand:: Rand ,
25
29
} ;
26
30
27
31
pub const ID : u32 = 0x0031 ;
28
32
33
+ #[ derive( FromRepr ) ]
34
+ #[ repr( u16 ) ]
35
+ pub enum Attributes {
36
+ MaxNetworks = 0x00 ,
37
+ Networks = 0x01 ,
38
+ ConnectMaxTimeSecs = 0x03 ,
39
+ InterfaceEnabled = 0x04 ,
40
+ LastNetworkingStatus = 0x05 ,
41
+ LastNetworkID = 0x06 ,
42
+ LastConnectErrorValue = 0x07 ,
43
+ }
44
+
45
+ attribute_enum ! ( Attributes ) ;
46
+
29
47
enum FeatureMap {
30
48
_Wifi = 0x01 ,
31
49
_Thread = 0x02 ,
@@ -35,7 +53,33 @@ enum FeatureMap {
35
53
pub const CLUSTER : Cluster < ' static > = Cluster {
36
54
id : ID as _ ,
37
55
feature_map : FeatureMap :: Ethernet as _ ,
38
- attributes : & [ FEATURE_MAP , ATTRIBUTE_LIST ] ,
56
+ attributes : & [
57
+ FEATURE_MAP ,
58
+ ATTRIBUTE_LIST ,
59
+ Attribute :: new ( Attributes :: MaxNetworks as u16 , Access :: RA , Quality :: F ) ,
60
+ Attribute :: new ( Attributes :: Networks as u16 , Access :: RA , Quality :: NONE ) ,
61
+ Attribute :: new (
62
+ Attributes :: ConnectMaxTimeSecs as u16 ,
63
+ Access :: RV ,
64
+ Quality :: F ,
65
+ ) ,
66
+ Attribute :: new (
67
+ Attributes :: InterfaceEnabled as u16 ,
68
+ Access :: RWVA ,
69
+ Quality :: N ,
70
+ ) ,
71
+ Attribute :: new (
72
+ Attributes :: LastNetworkingStatus as u16 ,
73
+ Access :: RA ,
74
+ Quality :: X ,
75
+ ) ,
76
+ Attribute :: new ( Attributes :: LastNetworkID as u16 , Access :: RA , Quality :: X ) ,
77
+ Attribute :: new (
78
+ Attributes :: LastConnectErrorValue as u16 ,
79
+ Access :: RA ,
80
+ Quality :: X ,
81
+ ) ,
82
+ ] ,
39
83
commands : & [ ] ,
40
84
} ;
41
85
@@ -49,15 +93,90 @@ impl NwCommCluster {
49
93
data_ver : Dataver :: new ( rand) ,
50
94
}
51
95
}
96
+
97
+ fn get_network_info ( & self ) -> NwMetaInfo < ' static > {
98
+ // Only single, Ethernet, supported for now
99
+ let nw_info = NwInfo {
100
+ network_id : OctetStr :: new ( b"en0" ) ,
101
+ connected : true ,
102
+ } ;
103
+ NwMetaInfo {
104
+ nw_info,
105
+ connect_max_time_secs : 60 ,
106
+ interface_enabled : true ,
107
+ last_nw_status : NetworkCommissioningStatus :: Success ,
108
+ }
109
+ }
110
+ }
111
+
112
+ #[ derive( ToTLV ) ]
113
+ struct NwInfo < ' a > {
114
+ network_id : OctetStr < ' a > ,
115
+ connected : bool ,
116
+ }
117
+
118
+ struct NwMetaInfo < ' a > {
119
+ nw_info : NwInfo < ' a > ,
120
+ connect_max_time_secs : u8 ,
121
+ interface_enabled : bool ,
122
+ last_nw_status : NetworkCommissioningStatus ,
123
+ }
124
+
125
+ #[ allow( dead_code) ]
126
+ enum NetworkCommissioningStatus {
127
+ Success = 0 ,
128
+ OutOfRange = 1 ,
129
+ BoundsExceeded = 2 ,
130
+ NetworkIDNotFound = 3 ,
131
+ DuplicateNetworkID = 4 ,
132
+ NetworkNotFound = 5 ,
133
+ RegulatoryError = 6 ,
134
+ AuthFailure = 7 ,
135
+ UnsupportedSecurity = 8 ,
136
+ OtherConnectionFailure = 9 ,
137
+ IPV6Failed = 10 ,
138
+ IPBindFailed = 11 ,
139
+ UnknownError = 12 ,
52
140
}
53
141
54
142
impl Handler for NwCommCluster {
55
143
fn read ( & self , attr : & AttrDetails , encoder : AttrDataEncoder ) -> Result < ( ) , Error > {
56
- if let Some ( writer) = encoder. with_dataver ( self . data_ver . get ( ) ) ? {
144
+ let info = self . get_network_info ( ) ;
145
+ if let Some ( mut writer) = encoder. with_dataver ( self . data_ver . get ( ) ) ? {
57
146
if attr. is_system ( ) {
58
147
CLUSTER . read ( attr. attr_id , writer)
59
148
} else {
60
- Err ( ErrorCode :: AttributeNotFound . into ( ) )
149
+ match attr. attr_id . try_into ( ) ? {
150
+ Attributes :: MaxNetworks => AttrType :: < u8 > :: new ( ) . encode ( writer, 1 ) ,
151
+ Attributes :: Networks => {
152
+ writer. start_array ( AttrDataWriter :: TAG ) ?;
153
+ info. nw_info . to_tlv ( & mut writer, TagType :: Anonymous ) ?;
154
+ writer. end_container ( ) ?;
155
+ writer. complete ( )
156
+ }
157
+ Attributes :: ConnectMaxTimeSecs => {
158
+ AttrType :: < u8 > :: new ( ) . encode ( writer, info. connect_max_time_secs )
159
+ }
160
+
161
+ Attributes :: InterfaceEnabled => {
162
+ AttrType :: < bool > :: new ( ) . encode ( writer, info. interface_enabled )
163
+ }
164
+
165
+ Attributes :: LastNetworkingStatus => {
166
+ AttrType :: < u8 > :: new ( ) . encode ( writer, info. last_nw_status as u8 )
167
+ }
168
+
169
+ Attributes :: LastNetworkID => {
170
+ info. nw_info
171
+ . network_id
172
+ . to_tlv ( & mut writer, AttrDataWriter :: TAG ) ?;
173
+ writer. complete ( )
174
+ }
175
+ Attributes :: LastConnectErrorValue => {
176
+ writer. null ( AttrDataWriter :: TAG ) ?;
177
+ writer. complete ( )
178
+ }
179
+ }
61
180
}
62
181
} else {
63
182
Ok ( ( ) )
0 commit comments