1use crate::clusters::generated::electrical_energy_measurement as spec_energy;
31use crate::clusters::generated::electrical_power_measurement as spec_power;
32use crate::clusters::generated::power_topology as spec_topology;
33use crate::dm::spec::{Conforming, Optional};
34use crate::dm::{Resolved, ResolvedCommand};
35use crate::im::{ClusterHandler, ClusterId, CommandId, InteractionContext, Status, StatusIb};
36use crate::tlv::{Tag, TlvWriter, ToTlv};
37
38use super::Cluster;
39
40pub mod power {
42 pub use super::spec_power::attribute::{
43 ACCURACY, ACTIVE_CURRENT, ACTIVE_POWER, APPARENT_CURRENT, APPARENT_POWER, FREQUENCY,
44 HARMONIC_CURRENTS, HARMONIC_PHASES, NEUTRAL_CURRENT, NUMBER_OF_MEASUREMENT_TYPES,
45 POWER_FACTOR, POWER_MODE, RANGES, REACTIVE_CURRENT, REACTIVE_POWER, RMS_CURRENT, RMS_POWER,
46 RMS_VOLTAGE, VOLTAGE,
47 };
48 pub use super::spec_power::event::MEASUREMENT_PERIOD_RANGES;
49 pub use super::spec_power::{
50 HarmonicMeasurementStruct, ID, MeasurementAccuracyRangeStruct, MeasurementAccuracyStruct,
51 MeasurementRangeStruct, MeasurementTypeEnum, PICS, PowerModeEnum, REVISION, feature,
52 };
53}
54
55pub mod energy {
57 pub use super::spec_energy::attribute::{
58 ACCURACY, CUMULATIVE_ENERGY_EXPORTED, CUMULATIVE_ENERGY_IMPORTED, CUMULATIVE_ENERGY_RESET,
59 PERIODIC_ENERGY_EXPORTED, PERIODIC_ENERGY_IMPORTED,
60 };
61 pub use super::spec_energy::event::{CUMULATIVE_ENERGY_MEASURED, PERIODIC_ENERGY_MEASURED};
62 pub use super::spec_energy::{
63 CumulativeEnergyResetStruct, EnergyMeasurementStruct, ID, MeasurementAccuracyStruct, PICS,
64 REVISION, feature,
65 };
66}
67
68pub trait PowerMeterHooks {
74 fn power_mode(&self) -> power::PowerModeEnum {
76 power::PowerModeEnum::Unknown
77 }
78
79 fn accuracy(&self) -> &[power::MeasurementAccuracyStruct<'_>];
85
86 fn ranges(&self) -> &[power::MeasurementRangeStruct] {
88 &[]
89 }
90
91 fn voltage(&self) -> Option<i64> {
93 None
94 }
95
96 fn active_current(&self) -> Option<i64> {
98 None
99 }
100
101 fn reactive_current(&self) -> Option<i64> {
103 None
104 }
105
106 fn apparent_current(&self) -> Option<i64> {
108 None
109 }
110
111 fn active_power(&self) -> Option<i64>;
116
117 fn reactive_power(&self) -> Option<i64> {
119 None
120 }
121
122 fn apparent_power(&self) -> Option<i64> {
124 None
125 }
126
127 fn rms_voltage(&self) -> Option<i64> {
129 None
130 }
131
132 fn rms_current(&self) -> Option<i64> {
134 None
135 }
136
137 fn rms_power(&self) -> Option<i64> {
139 None
140 }
141
142 fn frequency(&self) -> Option<i64> {
144 None
145 }
146
147 fn power_factor(&self) -> Option<i64> {
149 None
150 }
151
152 fn neutral_current(&self) -> Option<i64> {
154 None
155 }
156
157 fn harmonic_currents(&self) -> &[power::HarmonicMeasurementStruct] {
159 &[]
160 }
161
162 fn harmonic_phases(&self) -> &[power::HarmonicMeasurementStruct] {
164 &[]
165 }
166}
167
168#[derive(Debug)]
170pub struct ElectricalPowerMeasurement<'a, H: PowerMeterHooks> {
171 hooks: &'a H,
172}
173
174impl<'a, H: PowerMeterHooks> ElectricalPowerMeasurement<'a, H> {
175 #[must_use]
177 pub const fn new(hooks: &'a H) -> Self {
178 Self { hooks }
179 }
180
181 pub fn conforming(
183 feature_map: u32,
184 optional: &Optional<'_>,
185 ) -> crate::error::Result<Conforming<19, 0, 0, 1>> {
186 Conforming::new(&spec_power::CLUSTER, feature_map, optional)
187 }
188
189 pub const WITH_BASIC_READINGS: Optional<'static> = Optional {
191 attributes: &[
192 power::RANGES,
193 power::VOLTAGE,
194 power::ACTIVE_CURRENT,
195 power::APPARENT_CURRENT,
196 ],
197 commands: &[],
198 events: &[],
199 };
200}
201
202impl<H: PowerMeterHooks> ClusterHandler for ElectricalPowerMeasurement<'_, H> {
203 fn read(
204 &self,
205 resolved: &Resolved<'_>,
206 _ctx: &InteractionContext<'_>,
207 w: &mut TlvWriter<'_>,
208 tag: Tag,
209 ) -> Result<(), Status> {
210 let full = |r: crate::error::Result<()>| r.map_err(|_| Status::ResourceExhausted);
211 let measured = |w: &mut TlvWriter<'_>, value: Option<i64>| match value {
212 Some(value) => full(w.signed(tag, value)),
213 None => full(w.null(tag)),
214 };
215 let list = |w: &mut TlvWriter<'_>, items: &[power::HarmonicMeasurementStruct]| {
216 full(w.start_array(tag))?;
217 for item in items {
218 full(item.to_tlv(w, Tag::Anonymous))?;
219 }
220 full(w.end_container())
221 };
222 match resolved.attribute {
223 power::POWER_MODE => full(w.unsigned(tag, u64::from(self.hooks.power_mode().value()))),
224 power::NUMBER_OF_MEASUREMENT_TYPES => full(w.unsigned(
227 tag,
228 u64::try_from(self.hooks.accuracy().len()).unwrap_or(u64::MAX),
229 )),
230 power::ACCURACY => {
231 full(w.start_array(tag))?;
232 for entry in self.hooks.accuracy() {
233 full(entry.to_tlv(w, Tag::Anonymous))?;
234 }
235 full(w.end_container())
236 }
237 power::RANGES => {
238 full(w.start_array(tag))?;
239 for entry in self.hooks.ranges() {
240 full(entry.to_tlv(w, Tag::Anonymous))?;
241 }
242 full(w.end_container())
243 }
244 power::VOLTAGE => measured(w, self.hooks.voltage()),
245 power::ACTIVE_CURRENT => measured(w, self.hooks.active_current()),
246 power::REACTIVE_CURRENT => measured(w, self.hooks.reactive_current()),
247 power::APPARENT_CURRENT => measured(w, self.hooks.apparent_current()),
248 power::ACTIVE_POWER => measured(w, self.hooks.active_power()),
249 power::REACTIVE_POWER => measured(w, self.hooks.reactive_power()),
250 power::APPARENT_POWER => measured(w, self.hooks.apparent_power()),
251 power::RMS_VOLTAGE => measured(w, self.hooks.rms_voltage()),
252 power::RMS_CURRENT => measured(w, self.hooks.rms_current()),
253 power::RMS_POWER => measured(w, self.hooks.rms_power()),
254 power::FREQUENCY => measured(w, self.hooks.frequency()),
255 power::POWER_FACTOR => measured(w, self.hooks.power_factor()),
256 power::NEUTRAL_CURRENT => measured(w, self.hooks.neutral_current()),
257 power::HARMONIC_CURRENTS => list(w, self.hooks.harmonic_currents()),
258 power::HARMONIC_PHASES => list(w, self.hooks.harmonic_phases()),
259 _ => Err(Status::UnsupportedAttribute),
260 }
261 }
262
263 fn invoke(
264 &self,
265 _resolved: &ResolvedCommand<'_>,
266 _fields: Option<&[u8]>,
267 _ctx: &InteractionContext<'_>,
268 _w: &mut TlvWriter<'_>,
269 _tag: Tag,
270 ) -> Result<Option<CommandId>, StatusIb> {
271 Err(Status::UnsupportedCommand.into())
273 }
274}
275
276impl<H: PowerMeterHooks> Cluster for ElectricalPowerMeasurement<'_, H> {
277 const ID: ClusterId = power::ID;
278}
279
280pub trait EnergyMeterHooks {
282 fn accuracy(&self) -> energy::MeasurementAccuracyStruct<'_>;
284
285 fn cumulative_imported(&self) -> Option<energy::EnergyMeasurementStruct> {
287 None
288 }
289
290 fn cumulative_exported(&self) -> Option<energy::EnergyMeasurementStruct> {
292 None
293 }
294
295 fn periodic_imported(&self) -> Option<energy::EnergyMeasurementStruct> {
301 None
302 }
303
304 fn periodic_exported(&self) -> Option<energy::EnergyMeasurementStruct> {
306 None
307 }
308
309 fn cumulative_reset(&self) -> Option<energy::CumulativeEnergyResetStruct> {
314 None
315 }
316}
317
318#[derive(Debug)]
320pub struct ElectricalEnergyMeasurement<'a, H: EnergyMeterHooks> {
321 hooks: &'a H,
322}
323
324impl<'a, H: EnergyMeterHooks> ElectricalEnergyMeasurement<'a, H> {
325 #[must_use]
327 pub const fn new(hooks: &'a H) -> Self {
328 Self { hooks }
329 }
330
331 pub fn conforming(
333 feature_map: u32,
334 optional: &Optional<'_>,
335 ) -> crate::error::Result<Conforming<6, 0, 0, 2>> {
336 Conforming::new(&spec_energy::CLUSTER, feature_map, optional)
337 }
338}
339
340impl<H: EnergyMeterHooks> ClusterHandler for ElectricalEnergyMeasurement<'_, H> {
341 fn read(
342 &self,
343 resolved: &Resolved<'_>,
344 _ctx: &InteractionContext<'_>,
345 w: &mut TlvWriter<'_>,
346 tag: Tag,
347 ) -> Result<(), Status> {
348 let full = |r: crate::error::Result<()>| r.map_err(|_| Status::ResourceExhausted);
349 let reading =
350 |w: &mut TlvWriter<'_>, value: Option<energy::EnergyMeasurementStruct>| match value {
351 Some(value) => full(value.to_tlv(w, tag)),
352 None => full(w.null(tag)),
353 };
354 match resolved.attribute {
355 energy::ACCURACY => full(self.hooks.accuracy().to_tlv(w, tag)),
356 energy::CUMULATIVE_ENERGY_IMPORTED => reading(w, self.hooks.cumulative_imported()),
357 energy::CUMULATIVE_ENERGY_EXPORTED => reading(w, self.hooks.cumulative_exported()),
358 energy::PERIODIC_ENERGY_IMPORTED => reading(w, self.hooks.periodic_imported()),
359 energy::PERIODIC_ENERGY_EXPORTED => reading(w, self.hooks.periodic_exported()),
360 energy::CUMULATIVE_ENERGY_RESET => match self.hooks.cumulative_reset() {
361 Some(reset) => full(reset.to_tlv(w, tag)),
362 None => full(w.null(tag)),
363 },
364 _ => Err(Status::UnsupportedAttribute),
365 }
366 }
367
368 fn invoke(
369 &self,
370 _resolved: &ResolvedCommand<'_>,
371 _fields: Option<&[u8]>,
372 _ctx: &InteractionContext<'_>,
373 _w: &mut TlvWriter<'_>,
374 _tag: Tag,
375 ) -> Result<Option<CommandId>, StatusIb> {
376 Err(Status::UnsupportedCommand.into())
377 }
378}
379
380impl<H: EnergyMeterHooks> Cluster for ElectricalEnergyMeasurement<'_, H> {
381 const ID: ClusterId = energy::ID;
382}
383
384pub mod topology {
388 pub use super::spec_topology::attribute::{ACTIVE_ENDPOINTS, AVAILABLE_ENDPOINTS};
389 pub use super::spec_topology::{ID, PICS, REVISION, feature};
390}
391
392#[derive(Debug)]
406pub struct PowerTopology<'a> {
407 available: &'a [crate::im::EndpointId],
408 active: core::cell::RefCell<&'a [crate::im::EndpointId]>,
409}
410
411impl<'a> PowerTopology<'a> {
412 #[must_use]
417 pub const fn new(available: &'a [crate::im::EndpointId]) -> Self {
418 Self {
419 available,
420 active: core::cell::RefCell::new(available),
421 }
422 }
423
424 pub fn conforming(
426 feature_map: u32,
427 optional: &Optional<'_>,
428 ) -> crate::error::Result<Conforming<2, 0, 0, 0>> {
429 Conforming::new(&spec_topology::CLUSTER, feature_map, optional)
430 }
431
432 pub fn set_active(&self, active: &'a [crate::im::EndpointId]) -> Result<(), Status> {
438 if !active.iter().all(|id| self.available.contains(id)) {
439 return Err(Status::ConstraintError);
440 }
441 *self.active.borrow_mut() = active;
442 Ok(())
443 }
444
445 #[must_use]
447 pub fn active(&self) -> &'a [crate::im::EndpointId] {
448 *self.active.borrow()
449 }
450}
451
452impl ClusterHandler for PowerTopology<'_> {
453 fn read(
454 &self,
455 resolved: &Resolved<'_>,
456 _ctx: &InteractionContext<'_>,
457 w: &mut TlvWriter<'_>,
458 tag: Tag,
459 ) -> Result<(), Status> {
460 let full = |r: crate::error::Result<()>| r.map_err(|_| Status::ResourceExhausted);
461 let endpoints = |w: &mut TlvWriter<'_>, ids: &[crate::im::EndpointId]| {
462 full(w.start_array(tag))?;
463 for id in ids {
464 full(w.unsigned(Tag::Anonymous, u64::from(*id)))?;
465 }
466 full(w.end_container())
467 };
468 match resolved.attribute {
469 topology::AVAILABLE_ENDPOINTS => endpoints(w, self.available),
470 topology::ACTIVE_ENDPOINTS => endpoints(w, self.active()),
471 _ => Err(Status::UnsupportedAttribute),
472 }
473 }
474
475 fn invoke(
476 &self,
477 _resolved: &ResolvedCommand<'_>,
478 _fields: Option<&[u8]>,
479 _ctx: &InteractionContext<'_>,
480 _w: &mut TlvWriter<'_>,
481 _tag: Tag,
482 ) -> Result<Option<CommandId>, StatusIb> {
483 Err(Status::UnsupportedCommand.into())
484 }
485}
486
487impl Cluster for PowerTopology<'_> {
488 const ID: ClusterId = topology::ID;
489}