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_WIFI_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
105 RSP_SUCCESS = 0x0000,
106 RSP_SUCCESS_MORE = 0x0100,
108 RSP_INVALID_REQ = 0x0001,
109 RSP_INVALID_PARAM = 0x0002,
111 RSP_LOCKED_USER = 0x0004,
112 RSP_LOCKED_MASTER = 0x0005,
113 RSP_CANNOT_UNLOCK = 0x0006,
114 RSP_NOT_FOUND = 0x0007,
116 RSP_TIMEOUT = 0x0008,
117 RSP_INVALID = 0x0009,
118 RSP_CANCELLED = 0x000A,
153 uint8_t channel_id[CHANNEL_ID_SIZE];
154 uint8_t encryption_key[ENCRYPTION_KEY_SIZE];
173 uint8_t ssid[MAX_WIFI_SSID_SIZE];
176 uint8_t pass[MAX_WIFI_PASSWORD_SIZE];
188 uint8_t ssid[MAX_WIFI_SSID_SIZE];
216 uint16_t firmware_version_bcd;
217 uint8_t hardware_revision_bcd;
223 uint8_t ssid[MAX_WIFI_SSID_SIZE];
226 bool isUserPasswordSet(
void)
const;
227 bool isMasterPasswordSet(
void)
const;
228 bool isUserLocked(
void)
const;
229 bool isMasterLocked(
void)
const;
231 bool isWifiConnecting(
void)
const;
232 bool isWifiConnected(
void)
const;
233 bool isWifiFailed(
void)
const;
315 float peak_charge_current;
316 float peak_discharge_current;
328 uint32_t time_since_last_full_charge;
329 float full_charge_capacity;
331 uint64_t total_discharge;
332 uint64_t total_discharge_energy;
334 uint64_t total_charge;
335 uint64_t total_charge_energy;
339 float max_discharge_current;
340 float max_charge_current;
342 float deepest_discharge;
343 float last_discharge;
700 virtual void requestReadFile(uint32_t file_id, uint32_t offset, uint32_t read_size,
const std::function<
void(
ResponseCode,
const uint8_t*,
size_t)> &cb) = 0;
711 virtual void requestUpdateFirmware(
const uint8_t* firmware_image, uint32_t size,
const std::function<
bool(uint32_t, uint32_t)> &progress_cb,
768 for(uint32_t i = 0; i < 4; i++)
770 if ((bcd & 0xF) > 0x9)
793 static uint16_t
checkFirmwareImage(
const uint8_t* image,
size_t size, uint8_t hardware_revision_bcd);
797#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:70
@ FAMILY_MASK
Mask for the family part of the revision number.
Definition powermon.h:66
@ POWERMON
Original PowerMon (BLE)
Definition powermon.h:68
@ POWERMON_5S
PowerMon-5S (BLE)
Definition powermon.h:69
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.
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:126
@ PS_LTD
Power status is Low Temperature Disconnect.
Definition powermon.h:134
@ PS_ON
Power status is On.
Definition powermon.h:128
@ PS_FGD
Power status is Fuel Gauge Disconnect.
Definition powermon.h:132
@ PS_HTD
Power status is High Temperature Disconnect.
Definition powermon.h:135
@ PS_NCH
Power status is Not CHarging (for LiFePO4 charge manager only)
Definition powermon.h:133
@ PS_OCD
Power status is Over-Current Disconnect.
Definition powermon.h:130
@ PS_LVD
Power status is Low Voltage Disconnect.
Definition powermon.h:129
@ PS_OFF
Power status is Off.
Definition powermon.h:127
@ PS_HVD
Power status is High Voltage Disconnect.
Definition powermon.h:131
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.
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 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:89
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 requestReadFile(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.
static bool checkBCD(uint16_t bcd)
Returns true if the parameter is a valid BCD number.
Definition powermon.h:766
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.
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:104
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 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 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: BLE or WiFi.
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 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 requestGetFileList(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:78
@ Connecting
Device is in the process of connecting.
Definition powermon.h:80
@ Disconnected
Device is not connected.
Definition powermon.h:79
@ Connected
Device is connected.
Definition powermon.h:81
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 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:143
DeviceIdentifier is a structure containing all information used to identify a PowerMon device (either...
Definition powermon.h:241
uint64_t serial
Device serial number.
Definition powermon.h:243
uint8_t hardware_revision_bcd
Identifies if it's a WiFi or BLE device.
Definition powermon.h:244
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:247
std::string name
Device name.
Definition powermon.h:242
uint64_t address
contains the BLE MAC address for a BLE device of the IP address of a local WiFi device.
Definition powermon.h:245
DeviceInfo is the information structure returned by PowerMon as response to the GetInfo request.
Definition powermon.h:213
FuelgaugeStatistics is a structure containing battery statistics.
Definition powermon.h:327
LogFileDescriptor represents a PowerMon log file.
Definition powermon.h:354
uint32_t id
ID of the file (timestamp in UNIX time of the first sample in the file)
Definition powermon.h:355
uint32_t size
Size in bytes.
Definition powermon.h:356
MonitorData is a structure containing the real-time PowerMon data.
Definition powermon.h:272
float power
Power in Watts. Depending on the configuration, either V1 or V2 are used to compute this value.
Definition powermon.h:282
float temperature
Temperature in Celsius.
Definition powermon.h:283
uint16_t firmware_version_bcd
current firmware version in BCD format xx.yy
Definition powermon.h:273
int64_t energy_meter
Energy meter in mWh.
Definition powermon.h:286
uint16_t fg_runtime
Fuelgauge runtime in minutes. 0xFFFF means the FG is not enabled. The maximum valid value is 0xFFF0.
Definition powermon.h:291
float current
Current in Amperes.
Definition powermon.h:281
float voltage2
Voltage 2 in Volts.
Definition powermon.h:280
int64_t coulomb_meter
Coulomb meter in mAh.
Definition powermon.h:285
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:293
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:290
float voltage1
Voltage 1 in Volts.
Definition powermon.h:279
uint32_t flags
various flags - do not use directly
Definition powermon.h:277
uint8_t hardware_revision_bcd
current hardware revision in BCD format x.y
Definition powermon.h:274
uint32_t time
UNIX time in localtime (not UTC)
Definition powermon.h:276
PowerStatus power_status
Device power status.
Definition powermon.h:288
MonitorStatistics is a structure containing power meter statistics.
Definition powermon.h:306
float temperature_max
in Celsius
Definition powermon.h:319
uint32_t seconds_since_on
number of seconds since PowerMon was powered up
Definition powermon.h:307
float temperature_min
in Celsius
Definition powermon.h:318
WifiAccessKey is a structure representing the access keys used to connect to a WiFi PowerMon remotely...
Definition powermon.h:152
WifiNetwork represents all information required by PowerMon to connect to an access point.
Definition powermon.h:162
SecurityMode
Definition powermon.h:164
@ WPA2_WPA1_PSK
Use WPA1 or WPA2.
Definition powermon.h:167
@ WPA2_PSK
Use only WPA2.
Definition powermon.h:168
@ WEP
Use WEP.
Definition powermon.h:166
@ WPA3_SAE
Use WPA3 (STA mode only)
Definition powermon.h:169
@ OPEN
No security.
Definition powermon.h:165
WifiScanResult represents a WiFi network detected by PowerMon during WiFi scanning.
Definition powermon.h:186
uint8_t unused
Reserved, set to zero.
Definition powermon.h:198
uint8_t wep
Network supports WEP.
Definition powermon.h:193
uint8_t psk
Network supports Personal authentication.
Definition powermon.h:199
uint8_t wpa3
Network supports WPA3. If multiple WPA bits are set the network supports mixed mode.
Definition powermon.h:196
uint8_t wpa2
Network supports WPA2. If both WPA bits are set the network supports mixed mode.
Definition powermon.h:195
uint8_t eap
Network supports Enterprise authentication.
Definition powermon.h:200
uint8_t wpa
Network supports WPA. If both WPA bits are set the network supports mixed mode.
Definition powermon.h:194
uint8_t pmf
Networks requires use of Protected Management Frames.
Definition powermon.h:197
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