diff --git a/examples/connect.cpp b/examples/connect.cpp index 8c69b6c..c3dd9ab 100644 --- a/examples/connect.cpp +++ b/examples/connect.cpp @@ -49,14 +49,14 @@ //Let's set our connect callback. This gets called when a connection is successfully established powermon->setOnConnectCallback([]() { - printf("\nDevice is connected\n"); fflush(stdout); + printf("\nDevice connected\n"); fflush(stdout); connected = true; }); //... and our disconnect callback. This gets called when a device gets disconnected or a connection attempt fails. powermon->setOnDisconnectCallback([](Powermon::DisconnectReason reason) { - printf("\nDevice is disconnected, reason: %u\n", reason); fflush(stdout); + printf("\nDevice disconnected, reason: %u\n", reason); fflush(stdout); disconnected = true; }); @@ -66,7 +66,7 @@ //Here are the options: // - we can connect to a Bluetooth LE device (PowerMon or PowerMon-5S) // - we can connect to a WiFi or Ethernet device located in the same network - we need an IP address - // - we can connect to a WiFi or Ethernet device located remotely - we need the Access Keys + // - we can connect to a WiFi or Ethernet device located remotely - we need the access keys @@ -92,7 +92,7 @@ printf("%02X", id.access_key.encryption_key[i]); //let's connect - powermon->connectWifi(id.access_key); + powermon->connectIp(id.access_key); } else { @@ -101,25 +101,34 @@ } - /* + //WIFI / ETHERNET LOCAL CONNECTION - struct in_addr ip_addr; - inet_pton(AF_INET, "192.168.1.230", &ip_addr); //use the IP address of your device. You can get that from the PowerMon advertisments. - powermon->connectWifi(ip_addr.s_addr); -*/ + //struct in_addr ip_addr; + //inet_pton(AF_INET, "192.168.1.230", &ip_addr); //use the IP address of your device. You can get that from the PowerMon advertisments. + //powermon->connectIp(ip_addr.s_addr); - - /* + //BLE CONNECTION - const uint64_t ble_mac_address = (uint64_t)0x123456789ABC; //pay attention to big endian vs little endian + //BLE devices send the monitor data as a notification twice a second. + //subscribe to the notification + powermon->setOnMonitorDataCallback([](const Powermon::MonitorData &data) + { + printf("\nV1: %.3fV, V2: %.3fV, I: %.3fA, P: %.2fW, Coulombs: %.3fAh, Energy: %.3fWh, PS: %s", + data.voltage1, data.voltage2, data.current, data.power, + data.coulomb_meter / 1000.0, data.energy_meter / 1000.0, + Powermon::getPowerStatusString(data.power_status).c_str() + ); + fflush(stdout); + }); + //use the MAC address of your device. You can get that from the PowerMon advertisments. - powermon->connectBle(ble_mac_address); -*/ + //const uint64_t ble_mac_address = (uint64_t)0xFBC46D6D5398; //pay attention to big endian vs little endian + //powermon->connectBle(ble_mac_address); - + printf("\nConnecting ... "); fflush(stdout); //wait to either connect or fail - while (!connected && !disconnected) + while(!should_exit() && !connected && !disconnected) usleep(10 * 1000); //as you noticed, the PowerMon API is asynchronous. This means, you make a request by calling a member function in @@ -128,7 +137,6 @@ //For this reason, in this example we need to keep the thread alive, hence the volatile bool connected, disconnected and ready. //In your application, chances are that it's event driven and the Powermon library will fit nicely in that environment - if (connected) { //this is our first request sent to the device @@ -158,27 +166,29 @@ while(ready == false) usleep(50 * 1000); //wait until we get a response - printf("\n\nMonitor Data"); - - while(!should_exit()) + while(!should_exit() && !disconnected) { - powermon->requestGetMonitorData([](Powermon::ResponseCode response, const Powermon::MonitorData& data) - { - if (response == Powermon::ResponseCode::RSP_SUCCESS) - { - printf("\nV1: %.3fV, V2: %.3fV, I: %.3fA, P: %.2fW, Coulombs: %.3fAh, Energy: %.3fWh, PS: %s", - data.voltage1, data.voltage2, data.current, data.power, - data.coulomb_meter / 1000.0, data.energy_meter / 1000.0, - Powermon::getPowerStatusString(data.power_status).c_str() - ); - } - else - { - printf("\nFailed to get monitor data. Response code: %u", response); - } + //network devices dont't send monitor data as a notification. Instead, it must be requested. + if (Powermon::hasNetwork(powermon->getLastDeviceInfo().hardware_revision_bcd)) + { + powermon->requestGetMonitorData([](Powermon::ResponseCode response, const Powermon::MonitorData& data) + { + if (response == Powermon::ResponseCode::RSP_SUCCESS) + { + printf("\nV1: %.3fV, V2: %.3fV, I: %.3fA, P: %.2fW, Coulombs: %.3fAh, Energy: %.3fWh, PS: %s", + data.voltage1, data.voltage2, data.current, data.power, + data.coulomb_meter / 1000.0, data.energy_meter / 1000.0, + Powermon::getPowerStatusString(data.power_status).c_str() + ); + } + else + { + printf("\nFailed to get monitor data. Response code: %u", response); + } - fflush(stdout); - }); + fflush(stdout); + }); + } //delay 2 seconds and check if we should exit every 10ms. //This is done so we can respond quickly to the user request of terminating the program. @@ -189,14 +199,12 @@ break; } - powermon->disconnect(); - } - - //wait for the device to disconnect. If the connection failed when trying to establish it, disconnected will already be true - while (!disconnected) - usleep(50 * 1000); + //wait for the device to disconnect. If the connection failed when trying to establish it, disconnected will already be true + while(!disconnected) + usleep(50 * 1000); + } delete powermon; diff --git a/examples/scan.cpp b/examples/scan.cpp index 75df263..803f24b 100644 --- a/examples/scan.cpp +++ b/examples/scan.cpp @@ -50,6 +50,11 @@ adv.serial, adv.firmware_version_bcd >> 8, adv.firmware_version_bcd & 0xFF); + if (Powermon::hasNetwork(adv.hardware_revision_bcd)) + printf("IP: %s", Powermon::getIpAddressString(adv.address).c_str()); + else + printf("MAC: %s", Powermon::getMacAddressString(adv.address).c_str()); + printf("\tVoltage1: %.3fV, Current: %.3fA, Power: %.2fW, Temperature: %.3fC\n", adv.voltage1, adv.current, adv.power, adv.temperature); fflush(stdout); diff --git a/inc/powermon.h b/inc/powermon.h index 91a4d13..e67cbdb 100644 --- a/inc/powermon.h +++ b/inc/powermon.h @@ -147,9 +147,9 @@ /** - * \brief WifiAccessKey is a structure representing the access keys used to connect to a WiFi/Ethernet PowerMon remotely (via the Internet) + * \brief RemoteAccessKey is a structure representing the access keys used to connect to a WiFi/Ethernet PowerMon remotely (via the Internet) */ - struct WifiAccessKey + struct RemoteAccessKey { uint8_t channel_id[CHANNEL_ID_SIZE]; uint8_t encryption_key[ENCRYPTION_KEY_SIZE]; @@ -202,7 +202,7 @@ uint8_t wpa : 1; ///< Network supports WPA. If both WPA bits are set the network supports mixed mode. uint8_t wpa2 : 1; ///< Network supports WPA2. If both WPA bits are set the network supports mixed mode. uint8_t wpa3 : 1; ///< Network supports WPA3. If multiple WPA bits are set the network supports mixed mode. - uint8_t pmf : 1; ///< Networks requires use of Protected Management Frames + uint8_t pmf : 1; ///< Network requires use of Protected Management Frames uint8_t unused : 1; ///< Reserved, set to zero uint8_t psk : 1; ///< Network supports Personal authentication uint8_t eap : 1; ///< Network supports Enterprise authentication @@ -253,7 +253,7 @@ uint8_t hardware_revision_bcd; /// &cb) = 0; @@ -467,7 +468,7 @@ /** - * \brief Requests the device monitor data. It only applies to the WiFi devices. + * \brief Requests the device monitor data. It only applies to the network devices (WiFi / Ethernet). * \param cb Lambda of type void(ResponseCode, const MonitorData&) that will be called to signal the result of the request */ virtual void requestGetMonitorData(const std::function &cb) = 0; @@ -585,7 +586,7 @@ /** * \brief Resets the PowerMon configuration to factory settings - * This will also clear the data log, reset the name, authentication keys and access keys (for WiFi devices). + * This will also clear the data log, reset the name, authentication keys and remote access keys (for network devices). * \param cb Lambda of type void(ResponseCode) that will be called to signal the result of the request. */ virtual void requestResetConfig(const std::function &cb) = 0; @@ -593,7 +594,7 @@ /** * \brief Requests renaming the PowerMon device - * \param name New name. For Bluetooth PowerMons the name is limited to 8 characters. For WiFi PowerMons the name can be up to 32 characters in length. + * \param name New name. For Bluetooth PowerMons the name is limited to 8 characters. For network PowerMons the name can be up to 32 characters in length. * \param cb Lambda of type void(ResponseCode) that will be called to signal the result of the request */ virtual void requestRename(const char* name, const std::function &cb) = 0; @@ -653,14 +654,14 @@ /** - * \brief Requests the WiFi access keys (only applies to the WiFi PowerMons). The access keys are used to remotely access the device. - * \param cb Lambda of type void(ResponseCode, const WifiAccessKey&) that will be called to signal the result of the request + * \brief Requests the network remote access keys (only applies to the network PowerMons). The access keys are used to remotely access the device. + * \param cb Lambda of type void(ResponseCode, const RemoteAccessKey&) that will be called to signal the result of the request */ - virtual void requestGetAccessKeys(const std::function &cb) = 0; + virtual void requestGetAccessKeys(const std::function &cb) = 0; /** - * \brief Requests resetting of the WiFi access keys (only applies to the WiFi PowerMons). This effectively severs the connection to all the paired clients. + * \brief Requests resetting of the network remote access keys (only applies to the network PowerMons). This effectively severs the connection to all the paired clients. * \param cb Lambda of type void(ResponseCode) that will be called to signal the result of the request */ virtual void requestResetAccessKeys(const std::function &cb) = 0; @@ -714,7 +715,7 @@ /** - * \brief Requests clearing of all schedule + * \brief Requests clearing of all schedules * \param cb Lambda of type void(ResponseCode) that will be called to signal the result of the request */ virtual void requestClearSchedules(const std::function &cb) = 0; @@ -745,7 +746,7 @@ /** - * \brief Requests committing the schedules to non-volatile memory + * \brief Requests clearing of all schedules * \param cb Lambda of type void(ResponseCode) that will be called to signal the result of the request */ virtual void requestClearLog(const std::function &cb) = 0; @@ -756,22 +757,18 @@ * \param firmware_image The firmware update image * \param size Size of the firmware update image * \param progress_cb Lambda of type bool(uint32_t progress, uint32_t total) that will be called regularly with updates about the progress. - * Returning false fronm the lambda will abort the update operation. + * Returning false from the lambda will abort the update operation. * \param done_cb Lambda of type void(ResponseCode) that will be called to signal the result of the request upon completion of the firmware update */ virtual void requestUpdateFirmware(const uint8_t* firmware_image, uint32_t size, const std::function &progress_cb, const std::function &done_cb) = 0; - virtual void requestReadDebug(uint32_t offset, uint32_t read_size, const std::function &cb) = 0; virtual void requestEraseDebug(const std::function &cb) = 0; - - virtual void requestReboot(const std::function &cb) = 0; - - + /** * \brief Returns the IP address as string */ @@ -821,7 +818,7 @@ /** - * \brief Returns true if the PowerMon described by the BCD hardware revision has an initegrated shunt + * \brief Returns true if the PowerMon described by the BCD hardware revision has an integrated shunt */ static bool hasIntegratedShunt(uint8_t bcd); @@ -839,7 +836,7 @@ /** - * \brief Returns true if the PowerMon described by the BCD hardware revision has Ethernet + * \brief Returns true if the PowerMon described by the BCD hardware revision has any network connection (WiFi or Ethernet) */ static bool hasNetwork(uint8_t bcd); diff --git a/inc/powermon_scanner.h b/inc/powermon_scanner.h index f45e4a2..dce1480 100644 --- a/inc/powermon_scanner.h +++ b/inc/powermon_scanner.h @@ -78,12 +78,6 @@ /** - * \brief Initialized the BLE adapter - * \return True if the BLE was initialized - */ - virtual bool initBle(void) = 0; - - /** * \brief Sets the callback to be called by the Powermon scanner when a new advertisement has been received * \param cb Lambda of type `void(const Advertisement&)` */ @@ -91,23 +85,27 @@ /** * \brief Starts scanning for WiFi device advertisements + * \return Returns true if the WiFi scanning was started successfully, false if not. */ - virtual void startWifiScan(void) = 0; + virtual bool startWifiScan(void) = 0; /** * \brief Stops scanning for WiFi device advertisements + * \return Returns true if the WiFi scanning was stopped successfully, false if not. */ - virtual void stopWifiScan(void) = 0; + virtual bool stopWifiScan(void) = 0; /** * Starts scanning for BLE device advertisements + * \return Returns true if the BLE scanning was started successfully, false if not. */ - virtual void startBleScan(void) = 0; + virtual bool startBleScan(void) = 0; /** * Stops scanning for BLE device advertisements + * \return Returns true if the BLE scanning was stopped successfully, false if not. */ - virtual void stopBleScan(void) = 0; + virtual bool stopBleScan(void) = 0; }; diff --git a/powermon_lib.a b/powermon_lib.a index ad08fb4..8e30c09 100644 --- a/powermon_lib.a +++ b/powermon_lib.a Binary files differ diff --git a/powermon_lib.lib b/powermon_lib.lib index 24a0f5d..3036b93 100644 --- a/powermon_lib.lib +++ b/powermon_lib.lib Binary files differ diff --git a/powermon_lib_pic.a b/powermon_lib_pic.a index 8adf1c0..ee15965 100644 --- a/powermon_lib_pic.a +++ b/powermon_lib_pic.a Binary files differ diff --git a/powermon_lib_rpi64.a b/powermon_lib_rpi64.a index 5fc4c4d..216f445 100644 --- a/powermon_lib_rpi64.a +++ b/powermon_lib_rpi64.a Binary files differ diff --git a/powermon_lib_rpi64_pic.a b/powermon_lib_rpi64_pic.a index e31b89d..c847dd6 100644 --- a/powermon_lib_rpi64_pic.a +++ b/powermon_lib_rpi64_pic.a Binary files differ diff --git a/powermon_libd.lib b/powermon_libd.lib index 4fa6222..0ce6717 100644 --- a/powermon_libd.lib +++ b/powermon_libd.lib Binary files differ