30#include <powermon_config.h>
31#include <powermon_schedule.h>
32#include <powermon_log.h>
35#define MAX_WIFI_SSID_SIZE 32
36#define MAX_WIFI_PASSWORD_SIZE 64
38#define CHANNEL_ID_SIZE 16
39#define ENCRYPTION_KEY_SIZE 32
41#define MAX_BLE_NAME_LENGTH 8
42#define MAX_NAME_LENGTH 32
44#define MAX_TIMER_NAME_LENGTH 16
45#define MAX_TIMER_COUNT 16
47#define FG_SOC_DISABLED 0xFF
48#define FG_SOC_UNKNOWN 0xFE
50#define FG_RUNTIME_DISABLED 0xFFFF
51#define FG_RUNTIME_UNKNOWN 0xFFFE
52#define FG_RUNTIME_MAX 0xFFF0
106 RSP_SUCCESS = 0x0000,
107 RSP_SUCCESS_MORE = 0x0100,
109 RSP_INVALID_REQ = 0x0001,
110 RSP_INVALID_PARAM = 0x0002,
112 RSP_LOCKED_USER = 0x0004,
113 RSP_LOCKED_MASTER = 0x0005,
114 RSP_CANNOT_UNLOCK = 0x0006,
115 RSP_NOT_FOUND = 0x0007,
117 RSP_TIMEOUT = 0x0008,
118 RSP_INVALID = 0x0009,
119 RSP_CANCELLED = 0x000A,
154 uint8_t channel_id[CHANNEL_ID_SIZE];
155 uint8_t encryption_key[ENCRYPTION_KEY_SIZE];
174 uint8_t ssid[MAX_WIFI_SSID_SIZE];
177 uint8_t pass[MAX_WIFI_PASSWORD_SIZE];
181 bool isPasswordProtected(
void)
const;
182 bool isMetered(
void)
const;
183 bool isFailed(
void)
const;
185 void setMetered(
bool enabled);
195 uint8_t ssid[MAX_WIFI_SSID_SIZE];
223 uint16_t firmware_version_bcd;
224 uint8_t hardware_revision_bcd;
230 uint8_t ssid[MAX_WIFI_SSID_SIZE];
234 bool isUserPasswordSet(
void)
const;
235 bool isMasterPasswordSet(
void)
const;
236 bool isUserLocked(
void)
const;
237 bool isMasterLocked(
void)
const;
239 bool isWifiConnecting(
void)
const;
240 bool isWifiConnected(
void)
const;
241 bool isWifiFailed(
void)
const;
248 struct DeviceIdentifier
259 bool operator==(
const DeviceIdentifier &rhs);
323 float peak_charge_current;
324 float peak_discharge_current;
336 uint32_t time_since_last_full_charge;
337 float full_charge_capacity;
339 uint64_t total_discharge;
340 uint64_t total_discharge_energy;
342 uint64_t total_charge;
343 uint64_t total_charge_energy;
347 float max_discharge_current;
348 float max_charge_current;
350 float deepest_discharge;
351 float last_discharge;
761 virtual void requestUpdateFirmware(
const uint8_t* firmware_image, uint32_t size,
const std::function<
bool(uint32_t, uint32_t)> &progress_cb,
766 virtual void requestReadDebug(uint32_t offset, uint32_t read_size,
const std::function<
void(
ResponseCode,
const uint8_t*,
size_t)> &cb) = 0;
767 virtual void requestEraseDebug(
const std::function<
void(
ResponseCode)> &cb) = 0;
770 virtual void requestReboot(
const std::function<
void(
ResponseCode)> &cb) = 0;
857 for(uint32_t i = 0; i < 4; i++)
859 if ((bcd & 0xF) > 0x9)
882 static uint16_t
checkFirmwareImage(
const uint8_t* image,
size_t size, uint8_t hardware_revision_bcd);
886#include <powermon_scanner.h>
Powermon is a class representing one PowerMon device (BLE or WiFi) and offers a set of functions for ...
Definition powermon.h:59
static uint64_t parseMacAddress(const char *address)
Parses a MAC address string.
virtual void requestResetAuthKey(std::function< void(ResponseCode)> cb)=0
Requests the reset of the device authentication key.
HardwareRevision
HardwareRevision definitions (2 digit BCD format)
Definition powermon.h:65
@ POWERMON_W
PowerMon-W (WiFi)
Definition powermon.h:71
@ FAMILY_MASK
Mask for the family part of the revision number.
Definition powermon.h:66
@ POWERMON
Original PowerMon (BLE)
Definition powermon.h:69
@ POWERMON_E
PowerMon-E (Ethernet)
Definition powermon.h:68
@ POWERMON_5S
PowerMon-5S (BLE)
Definition powermon.h:70
virtual void requestGetConfig(const std::function< void(ResponseCode, const PowermonConfig &)> &cb)=0
Retrieves the device configuration structure.
virtual void requestGetMonitorData(const std::function< void(ResponseCode, const MonitorData &)> &cb)=0
Requests the device monitor data. It only applies to the WiFi devices.
static bool hasEthernet(uint8_t bcd)
Returns true if the PowerMon described by the BCD hardware revision has Ethernet.
virtual void requestSetPowerState(bool state, const std::function< void(ResponseCode)> &cb)=0
Requests changing the power state.
virtual void requestStartWifiScan(const std::function< void(ResponseCode)> &cb)=0
Requests starting the WiFi scan (only applies to the WiFi PowerMons). WiFi scanning will stop automat...
PowerStatus
PowerStatus is an enumeration of all possible power states.
Definition powermon.h:127
@ PS_LTD
Power status is Low Temperature Disconnect.
Definition powermon.h:135
@ PS_ON
Power status is On.
Definition powermon.h:129
@ PS_FGD
Power status is Fuel Gauge Disconnect.
Definition powermon.h:133
@ PS_HTD
Power status is High Temperature Disconnect.
Definition powermon.h:136
@ PS_NCH
Power status is Not CHarging (for LiFePO4 charge manager only)
Definition powermon.h:134
@ PS_OCD
Power status is Over-Current Disconnect.
Definition powermon.h:131
@ PS_LVD
Power status is Low Voltage Disconnect.
Definition powermon.h:130
@ PS_OFF
Power status is Off.
Definition powermon.h:128
@ PS_HVD
Power status is High Voltage Disconnect.
Definition powermon.h:132
virtual void requestResetAccessKeys(const std::function< void(ResponseCode)> &cb)=0
Requests resetting of the WiFi access keys (only applies to the WiFi PowerMons). This effectively sev...
virtual void requestSetConfig(const PowermonConfig &config, const std::function< void(ResponseCode)> &cb)=0
Sends new configuration to the device.
static uint16_t getVersion(void)
Returns the PowerMon access library version.
virtual void requestUpdateSchedule(uint64_t old_schedule_descriptor, const PowermonSchedule &new_schedule, const std::function< void(ResponseCode)> &cb)=0
Requests updating an existing schedule.
virtual void requestClearSchedules(const std::function< void(ResponseCode)> &cb)=0
Requests clearing of all schedule.
virtual void connectWifi(uint32_t ipaddr)=0
Connects to a local WiFi PowerMon.
virtual bool initBle(void)=0
Initialized the BLE sub-system. This should be called once after creating the Powermon object.
virtual void requestGetStatistics(const std::function< void(ResponseCode, const MonitorStatistics &)> &cb)=0
Requests the device power monitor statistics data.
virtual void requestFgSynchronize(const std::function< void(ResponseCode)> &cb)=0
Requests forcing the SoC to 100% (SoC synchronize)
static std::string getPowerStatusString(PowerStatus ps)
Returns the power status string representation.
DisconnectReason
DisconnectReason is an enumeration of all reasons a connection can be terminated.
Definition powermon.h:90
virtual void requestWifiConfigure(const WifiNetwork &network, const std::function< void(ResponseCode)> &cb)=0
Sends new WiFi network credentials to the PowerMon device. The new credentials will be saved by the d...
virtual void requestGetAccessKeys(const std::function< void(ResponseCode, const WifiAccessKey &)> &cb)=0
Requests the WiFi access keys (only applies to the WiFi PowerMons). The access keys are used to remot...
virtual void requestSetUserPasswordLock(const AuthKey &key, std::function< void(ResponseCode)> cb)=0
Requests setting a user password lock.
static std::string getIpAddressString(uint32_t ip)
Returns the IP address as string.
virtual void connectWifi(const WifiAccessKey &key)=0
Connects to a remote WiFi PowerMon.
virtual void requestClearUserPasswordLock(std::function< void(ResponseCode)> cb)=0
Requests clearing of the user password lock.
virtual void requestRemoveWifiNetwork(uint8_t index, const std::function< void(ResponseCode)> &cb)=0
Removes a stored WiFi network.
static bool hasNetwork(uint8_t bcd)
Returns true if the PowerMon described by the BCD hardware revision has Ethernet.
static bool checkBCD(uint16_t bcd)
Returns true if the parameter is a valid BCD number.
Definition powermon.h:855
virtual void requestZeroCurrentOffset(const std::function< void(ResponseCode)> &cb)=0
Requests zeroing of the current reading offset.
virtual void requestAddSchedules(const std::vector< PowermonSchedule > &schedules, const std::function< void(ResponseCode)> &cb)=0
Requests adding new schedules.
static AuthKey getAuthKeyFromPassword(const char *password)
Generates the SHA256 hash of a password.
virtual const DeviceInfo & getLastDeviceInfo(void) const =0
Returns the last DeviceInfo retrieved from the PowerMon.
static bool hasBluetooth(uint8_t bcd)
Returns true if the PowerMon described by the BCD hardware revision has Bluetooth.
static bool hasDataLog(uint8_t bcd)
Returns true if the PowerMon described by the BCD hardware revision has data logging capabilities.
virtual void requestGetFgStatistics(const std::function< void(ResponseCode, const FuelgaugeStatistics &)> &cb)=0
Requests the device battery statistics data.
static bool hasWifi(uint8_t bcd)
Returns true if the PowerMon described by the BCD hardware revision has WiFi.
static std::string getMacAddressString(uint64_t mac)
Returns the Bluetooth MAC address as string.
ResponseCode
ResponseCodes is an enumeration of all possible response codes for requests to a PowerMon device.
Definition powermon.h:105
static bool hasVoltage2(uint8_t bcd)
Returns true if the PowerMon described by the BCD hardware revision has V2.
virtual void requestSetTime(uint32_t time, const std::function< void(ResponseCode)> &cb)=0
Requests setting the internal clock of the PowerMon device.
virtual void requestAddWifiNetwork(const WifiNetwork &network, const std::function< void(ResponseCode)> &cb)=0
Add a new WiFi network The new credentials will be saved by the device whether PowerMon can or cannot...
virtual void requestGetInfo(const std::function< void(ResponseCode, const DeviceInfo &)> &cb)=0
Requests the device information.
static Powermon * createInstance(void)
Creates an instance of the Powermon class.
virtual void requestCalibrateCurrent(float value, const std::function< void(ResponseCode)> &cb)=0
Requests calibration of the current reading.
virtual void requestDeleteSchedule(uint64_t schedule_descriptor, const std::function< void(ResponseCode)> &cb)=0
Requests deleting an existing schedule.
virtual void setOnConnectCallback(const std::function< void(void)> &cb)=0
Sets the callback to be called by the driver when a connection to the PowerMon device is fully establ...
virtual void requestUpdateFirmware(const uint8_t *firmware_image, uint32_t size, const std::function< bool(uint32_t, uint32_t)> &progress_cb, const std::function< void(ResponseCode)> &done_cb)=0
Requests firmware update.
virtual void setOnWifiScanReportCallback(const std::function< void(const WifiScanResult *)> &cb)=0
Sets the callback to be called by the driver when a new WiFi scan result is received from the PowerMo...
virtual void requestUnlock(const AuthKey &key, std::function< void(ResponseCode)> cb)=0
Requests unlocking a password protected device.
static bool hasIntegratedShunt(uint8_t bcd)
Returns true if the PowerMon described by the BCD hardware revision has an initegrated shunt.
static std::string getUpdateFirmwareImageUrl(uint8_t hardware_revision_bcd, uint16_t firmware_revision_bcd)
Returns the update firmware image URL based on the hardware revision and firmware version.
virtual void requestResetEnergyMeter(const std::function< void(ResponseCode)> &cb)=0
Requests the reset of the energy meter.
virtual void requestResetStatistics(const std::function< void(ResponseCode)> &cb)=0
Requests the reset of the power meter statistics.
virtual void requestRename(const char *name, const std::function< void(ResponseCode)> &cb)=0
Requests renaming the PowerMon device.
virtual bool isLocalConnection(void) const =0
Returns true if the current connection is local.
virtual void setOnDisconnectCallback(const std::function< void(DisconnectReason)> &cb)=0
Sets the callback to be called by the driver when a connection to the PowerMon device is disconnected...
static bool hasConfigurableShunt(uint8_t bcd)
Returns true if the PowerMon described by the BCD hardware revision supports configurable shunts.
virtual void requestClearMasterPasswordLock(std::function< void(ResponseCode)> cb)=0
Requests clearing of the master password lock.
virtual void requestClearLog(const std::function< void(ResponseCode)> &cb)=0
Requests committing the schedules to non-volatile memory.
virtual void requestGetSchedules(const std::function< void(ResponseCode, const std::vector< PowermonSchedule > &)> &cb)=0
Requests the list of all schedules stored in the device.
virtual void connectBle(uint64_t ble_address)=0
Connects to a local BLE PowerMon.
static std::string getHardwareString(uint8_t bcd)
Returns the hardware name based on the hardware revision in BCD format.
virtual void requestGetWifiNetworks(const std::function< void(ResponseCode, const std::vector< WifiNetwork > &)> &cb)=0
Returns a list of all WiFi networks known by the device.
virtual void requestGetLogFileList(const std::function< void(ResponseCode, const std::vector< LogFileDescriptor > &)> &cb)=0
Requests the list of log files.
State
State represents the PowerMon connection state.
Definition powermon.h:79
@ Connecting
Device is in the process of connecting.
Definition powermon.h:81
@ Disconnected
Device is not connected.
Definition powermon.h:80
@ Connected
Device is connected.
Definition powermon.h:82
virtual void requestResetConfig(const std::function< void(ResponseCode)> &cb)=0
Resets the PowerMon configuration to factory settings This will also clear the data log,...
virtual void requestSetMasterPasswordLock(const AuthKey &key, std::function< void(ResponseCode)> cb)=0
Requests setting a master password lock on.
virtual void requestGetAuthKey(std::function< void(ResponseCode, const AuthKey &)> cb)=0
Requests the authentication key from the device. This key can be used to unlock a locked device....
virtual void setOnMonitorDataCallback(const std::function< void(const MonitorData &)> &cb)=0
Sets the callback to be called by the driver when new monitor data is received. This applies to the B...
static uint16_t checkFirmwareImage(const uint8_t *image, size_t size, uint8_t hardware_revision_bcd)
Checks the validity of the firmware update image.
virtual void requestReadLogFile(uint32_t file_id, uint32_t offset, uint32_t read_size, const std::function< void(ResponseCode, const uint8_t *, size_t)> &cb)=0
Requests reading of a log file.
virtual void disconnect(void)=0
Disconnects from a connected device.
virtual void requestCommitSchedules(const std::function< void(ResponseCode)> &cb)=0
Requests committing the schedules to non-volatile memory.
virtual void requestResetCoulombMeter(const std::function< void(ResponseCode)> &cb)=0
Requests the reset of the coulomb meter.
AuthKey is a structure representing an authentication key used to unlock a locked PowerMon device.
Definition powermon.h:144
uint64_t serial
Device serial number.
Definition powermon.h:251
uint8_t hardware_revision_bcd
Identifies if it's a WiFi or BLE device.
Definition powermon.h:252
bool fromURL(const char *url)
Initializes the current object from a device link.
std::string toURL(void)
Saves the current PowerMon-W access structure to a link. Only remote access WiFi device identifiers c...
WifiAccessKey access_key
Contains the access keys for a remote WiFi device. In this case address is zero.
Definition powermon.h:255
std::string name
Device name.
Definition powermon.h:250
uint64_t address
contains the BLE MAC address for a BLE device of the IP address of a local WiFi device.
Definition powermon.h:253
DeviceInfo is the information structure returned by PowerMon as response to the GetInfo request.
Definition powermon.h:220
FuelgaugeStatistics is a structure containing battery statistics.
Definition powermon.h:335
LogFileDescriptor represents a PowerMon log file.
Definition powermon.h:362
uint32_t id
ID of the file (timestamp in UNIX time of the first sample in the file)
Definition powermon.h:363
uint32_t size
Size in bytes.
Definition powermon.h:364
MonitorData is a structure containing the real-time PowerMon data.
Definition powermon.h:280
float power
Power in Watts. Depending on the configuration, either V1 or V2 are used to compute this value.
Definition powermon.h:290
float temperature
Temperature in Celsius.
Definition powermon.h:291
uint16_t firmware_version_bcd
current firmware version in BCD format xx.yy
Definition powermon.h:281
int64_t energy_meter
Energy meter in mWh.
Definition powermon.h:294
uint16_t fg_runtime
Fuelgauge runtime in minutes. 0xFFFF means the FG is not enabled. The maximum valid value is 0xFFF0.
Definition powermon.h:299
float current
Current in Amperes.
Definition powermon.h:289
float voltage2
Voltage 2 in Volts.
Definition powermon.h:288
int64_t coulomb_meter
Coulomb meter in mAh.
Definition powermon.h:293
bool isTemperatureExternal(void) const
Returns true if the temperature is read from an external sensor.
int16_t rssi
RSSI as seen by the device in dBm.
Definition powermon.h:301
uint8_t fg_soc
Fuelgauge SoC (State of Charge) in percentage. 0xFF means the FG is not enabled. 0xFE means the SoC i...
Definition powermon.h:298
float voltage1
Voltage 1 in Volts.
Definition powermon.h:287
uint32_t flags
various flags - do not use directly
Definition powermon.h:285
uint8_t hardware_revision_bcd
current hardware revision in BCD format x.y
Definition powermon.h:282
uint32_t time
UNIX time in localtime (not UTC)
Definition powermon.h:284
PowerStatus power_status
Device power status.
Definition powermon.h:296
MonitorStatistics is a structure containing power meter statistics.
Definition powermon.h:314
float temperature_max
in Celsius
Definition powermon.h:327
uint32_t seconds_since_on
number of seconds since PowerMon was powered up
Definition powermon.h:315
float temperature_min
in Celsius
Definition powermon.h:326
WifiAccessKey is a structure representing the access keys used to connect to a WiFi/Ethernet PowerMon...
Definition powermon.h:153
WifiNetwork represents all information required by PowerMon-W to connect to an access point.
Definition powermon.h:163
SecurityMode
Definition powermon.h:165
@ WPA2_WPA1_PSK
Use WPA1 or WPA2.
Definition powermon.h:168
@ WPA2_PSK
Use only WPA2.
Definition powermon.h:169
@ WEP
Use WEP.
Definition powermon.h:167
@ WPA3_SAE
Use WPA3 (STA mode only)
Definition powermon.h:170
@ OPEN
No security.
Definition powermon.h:166
WifiScanResult represents a WiFi network detected by PowerMon-W during WiFi scanning.
Definition powermon.h:193
uint8_t unused
Reserved, set to zero.
Definition powermon.h:205
uint8_t wep
Network supports WEP.
Definition powermon.h:200
uint8_t psk
Network supports Personal authentication.
Definition powermon.h:206
uint8_t wpa3
Network supports WPA3. If multiple WPA bits are set the network supports mixed mode.
Definition powermon.h:203
uint8_t wpa2
Network supports WPA2. If both WPA bits are set the network supports mixed mode.
Definition powermon.h:202
uint8_t eap
Network supports Enterprise authentication.
Definition powermon.h:207
uint8_t wpa
Network supports WPA. If both WPA bits are set the network supports mixed mode.
Definition powermon.h:201
uint8_t pmf
Networks requires use of Protected Management Frames.
Definition powermon.h:204
PowermonConfig is a structure containing the PowerMon configuration.
Definition powermon_config.h:30
PowermonSchedule is a structure containing one PowerMon schedule (timer)
Definition powermon_schedule.h:30