Metering#

Functions

struct metering *metering_create(const metering_t type, const struct metering_param *param, metering_save_cb_t save_cb, void *save_cb_ctx)#

Creates a new metering instance.

Parameters:
  • type[in] Type of metering to create (see metering_t enum)

  • param[in] Metering parameters configuration

  • save_cb[in] Callback function for saving metering data

  • save_cb_ctx[in] Context pointer passed to save callback

Returns:

Pointer to created metering instance, NULL on failure

void metering_destroy(struct metering *self)#

Destroys a metering instance.

This function releases the resources associated with the given metering instance.

Parameters:

self[in] A pointer to the metering instance to be destroyed.

int metering_enable(struct metering *self)#

Enables the metering instance.

This function enables the metering process for the given metering instance.

Parameters:

self[in] A pointer to the metering instance to be enabled.

Returns:

0 on success, non-zero on failure.

int metering_disable(struct metering *self)#

Disables the metering instance.

This function disables the metering process for the given metering instance.

Parameters:

self[in] A pointer to the metering instance to be disabled.

Returns:

0 on success, non-zero on failure.

int metering_save_energy(struct metering *self)#

Saves the current metering energy data to non-volatile storage.

Parameters:

self – Pointer to the metering instance

Returns:

0 on success, negative errno on failure

int metering_step(struct metering *self)#

Executes a metering step.

This function performs a metering process using the step method of the metering_api structure associated with the given metering instance. This function is intended to be called periodically.

Note

Energy data is saved when either of these conditions is met:

  • Accumulated energy delta exceeds METERING_ENERGY_SAVE_THRESHOLD_WH

  • Time since last save exceeds METERING_ENERGY_SAVE_INTERVAL_MIN

Parameters:

self[in] A pointer to the metering instance.

Returns:

0 on success, non-zero on failure.

int metering_get_voltage(struct metering *self, int32_t *millivolt)#

Get the voltage measurement.

Parameters:
  • self[in] Pointer to the metering structure.

  • millivolt[out] Pointer to store the voltage in millivolts.

Returns:

0 on success, non-zero on failure.

int metering_get_current(struct metering *self, int32_t *milliamp)#

Get the current measurement.

Parameters:
  • self[in] Pointer to the metering structure.

  • milliamp[out] Pointer to store the current in milliamps.

Returns:

0 on success, non-zero on failure.

int metering_get_power_factor(struct metering *self, int32_t *centi)#

Get the power factor measurement.

Parameters:
  • self[in] Pointer to the metering structure.

  • centi[out] Pointer to store the power factor in centi-units.

Returns:

0 on success, non-zero on failure.

int metering_get_frequency(struct metering *self, int32_t *centihertz)#

Get the frequency measurement.

Parameters:
  • self[in] Pointer to the metering structure.

  • centihertz[out] Pointer to store the frequency in centihertz.

Returns:

0 on success, non-zero on failure.

int metering_get_phase(struct metering *self, int32_t *centidegree, int hz)#

Get the phase measurement.

Parameters:
  • self[in] Pointer to the metering structure.

  • centidegree[out] Pointer to store the phase in centidegrees.

  • hz[in] Frequency in hertz.

Returns:

0 on success, non-zero on failure.

int metering_get_energy(struct metering *self, uint64_t *wh, uint64_t *varh)#

Get the energy measurement.

Parameters:
  • self[in] Pointer to the metering structure.

  • wh[out] Pointer to store the energy in watt-hours.

  • varh[out] Pointer to store the reactive energy in volt-ampere reactive hours.

Returns:

0 on success, non-zero on failure.

int metering_get_power(struct metering *self, int32_t *watt, int32_t *var)#

Get the power measurement.

Parameters:
  • self[in] Pointer to the metering structure.

  • watt[out] Pointer to store the power in watts.

  • var[out] Pointer to store the reactive power in volt-amperes reactive.

Returns:

0 on success, non-zero on failure.

Typedefs

typedef bool (*metering_save_cb_t)(const struct metering *self, const struct metering_energy *energy, void *ctx)#

Callback function type for saving metering energy data.

Param self:

[in] Pointer to the metering instance

Param energy:

[in] Energy data to be saved

Param ctx:

[in] User context pointer passed during registration

Return:

true if save operation was successful, false otherwise

struct metering_io#
#include <metering.h>

Public Members

struct lm_uart *uart#
struct metering_energy#
#include <metering.h>

Public Members

uint64_t wh#

energy in watt-hour

uint64_t varh#

reactive energy in volt-ampere-reactive-hour

struct metering_param#
#include <metering.h>

Public Members

struct metering_io *io#

I/O configuration

struct metering_energy energy#

initial energy

struct metering_api#
#include <metering.h>

Public Members

void (*destroy)(struct metering *self)#
int (*enable)(struct metering *self)#
int (*disable)(struct metering *self)#
int (*step)(struct metering *self)#
int (*save_energy)(struct metering *self)#
int (*get_voltage)(struct metering *self, int32_t *millivolt)#
int (*get_current)(struct metering *self, int32_t *milliamp)#
int (*get_power_factor)(struct metering *self, int32_t *centi)#
int (*get_frequency)(struct metering *self, int32_t *centihertz)#
int (*get_phase)(struct metering *self, int32_t *centidegree, int hz)#
int (*get_energy)(struct metering *self, uint64_t *wh, uint64_t *varh)#
int (*get_power)(struct metering *self, int32_t *watt, int32_t *var)#

Enums

enum metering_t#

Values:

enumerator METERING_HLW811X#

Defines

METERING_ENERGY_SAVE_THRESHOLD_WH 1000 /* 1 kWh */#

Energy accumulation threshold in watt-hours that triggers a save Energy data will be saved if accumulated delta exceeds this value

METERING_ENERGY_SAVE_INTERVAL_MIN 5 /* 5 minutes */#

Minimum interval in minutes between energy data saves Energy data will be saved if this time has elapsed since last save