Skip to content

Commit

Permalink
Enable better printing support for Storage/IFS classes, BitSet, TRange (
Browse files Browse the repository at this point in the history
#2557)

- Classes don't need to be virtual to support `printTo` method
- Avoid `strlen` call in `Print::println()`
- Fix issue with streaming operator and char[]
- Replace String += operator overloads with template method
- Update HostTests using Serial instead of debugf()
- Add BitSet printTo()
- Update HostTests printing
- Revise Storage/IFS debug printing, making Device, Partition and various IFS classes printable
- Add TRange printing
  • Loading branch information
mikee47 authored Sep 26, 2022
1 parent 2ef58d1 commit d21bf5a
Show file tree
Hide file tree
Showing 31 changed files with 205 additions and 216 deletions.
4 changes: 2 additions & 2 deletions Sming/Components/Network/src/IpAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ using ip4_addr_t = ip_addr_t;
* @brief A class to make it easier to handle and pass around IP addresses
* @ingroup wiring
*/
class IpAddress : public Printable
class IpAddress
{
private:
ip_addr_t address{0}; ///< IPv4 address
Expand Down Expand Up @@ -191,7 +191,7 @@ class IpAddress : public Printable
return *this;
}

size_t printTo(Print& p) const override;
size_t printTo(Print& p) const;
};

inline String toString(IpAddress address)
Expand Down
4 changes: 2 additions & 2 deletions Sming/Components/Network/src/Network/Http/HttpParams.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* @ingroup http
*
*/
class HttpParams : public HashMap<String, String>, public Printable
class HttpParams : public HashMap<String, String>
{
public:
HttpParams() = default;
Expand Down Expand Up @@ -69,7 +69,7 @@ class HttpParams : public HashMap<String, String>, public Printable
}

// Printable
size_t printTo(Print& p) const override;
size_t printTo(Print& p) const;

/**
* @brief Printable output for debugging
Expand Down
4 changes: 2 additions & 2 deletions Sming/Components/Network/src/Network/Mqtt/MqttBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* @brief Helper class to simplify printing and parsing message buffers
*/
class MqttBuffer : public Printable
class MqttBuffer
{
public:
MqttBuffer(const mqtt_buffer_t& buf) : buf(buf)
Expand All @@ -28,7 +28,7 @@ class MqttBuffer : public Printable
return String(reinterpret_cast<const char*>(buf.data), buf.length);
}

size_t printTo(Print& p) const override
size_t printTo(Print& p) const
{
return p.write(buf.data, buf.length);
}
Expand Down
35 changes: 5 additions & 30 deletions Sming/Components/Storage/src/Debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,12 @@ namespace Storage
{
namespace Debug
{
void printPartition(Print& out, Partition part, bool includeDevice)
{
out.print(part.name());
if(includeDevice) {
out.print(" on ");
out.print(part.getDeviceName());
}
out.print(" (");
out.print(part.typeString());
out.print(_F(" @ 0x"));
out.print(part.address(), HEX);
out.print(_F(", size 0x"));
out.print(part.size(), HEX);
out.println(")");
}

void listPartitions(Print& out)
{
out.println();
out.println(_F("Registered partitions:"));
for(auto part : Storage::findPartition()) {
out.print("- ");
printPartition(out, part);
out << "- " << part << endl;
}
out.println();
}
Expand All @@ -38,26 +21,18 @@ void listDevices(Print& out, bool fullPartitionInfo)
out.println();
out.println(_F("Registered storage devices:"));
for(auto& dev : Storage::getDevices()) {
out.print(" name = '");
out.print(dev.getName());
out.print(_F("', type = "));
out.print(toString(dev.getType()));
out.print(_F(", size = 0x"));
out.print(dev.getSize(), HEX);
out.print(_F(", partitions:"));
if(dev.partitions().count() == 0) {
out << " " << dev << _F(". Partitions:");
if(!dev.partitions()) {
out.println(_F(" None."));
continue;
}

out.println();
for(auto part : dev.partitions()) {
if(fullPartitionInfo) {
out.print(" ");
printPartition(out, part, false);
out << " " << part << endl;
} else {
out.print(" ");
out.print(part.name());
out << " " << part.name();
}
}
out.println();
Expand Down
12 changes: 12 additions & 0 deletions Sming/Components/Storage/src/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "include/Storage/Device.h"
#include "include/Storage/partition_info.h"
#include <FlashString/Vector.hpp>
#include <Print.h>
#include <debug_progmem.h>

namespace
Expand Down Expand Up @@ -109,4 +110,15 @@ bool Device::loadPartitions(Device& source, uint32_t tableOffset)
return false;
}

size_t Device::printTo(Print& p) const
{
size_t n{0};
n += p.print(getName());
n += p.print(_F(": type "));
n += p.print(getType());
n += p.print(_F(", size 0x"));
n += p.print(getSize(), HEX);
return n;
}

} // namespace Storage
21 changes: 21 additions & 0 deletions Sming/Components/Storage/src/Partition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "include/Storage/Partition.h"
#include "include/Storage/Device.h"
#include <FlashString/Map.hpp>
#include <Print.h>
#include <debug_progmem.h>

using namespace Storage;
Expand Down Expand Up @@ -248,4 +249,24 @@ bool Partition::erase_range(uint32_t offset, size_t size)
return mDevice ? mDevice->erase_range(addr, size) : false;
}

size_t Partition::printTo(Print& p) const
{
size_t n{0};
if(*this) {
n += p.print(getDeviceName());
n += p.print('/');
n += p.print(name());
n += p.print(" (");
n += p.print(typeString());
n += p.print(" @ 0x");
n += p.print(address(), HEX);
n += p.print(_F(", size 0x"));
n += p.print(size(), HEX);
n += p.print(')');
} else {
n += p.print(_F("(none)"));
}
return n;
}

} // namespace Storage
1 change: 0 additions & 1 deletion Sming/Components/Storage/src/include/Storage/Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace Storage
{
namespace Debug
{
void printPartition(Print& out, Partition part, bool includeDevice = true);
void listPartitions(Print& out);
void listDevices(Print& out, bool fullPartitionInfo = true);

Expand Down
3 changes: 3 additions & 0 deletions Sming/Components/Storage/src/include/Storage/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#pragma once

#include <WString.h>
#include <Printable.h>
#include <Data/LinkedObjectList.h>
#include "PartitionTable.h"

Expand Down Expand Up @@ -139,6 +140,8 @@ class Device : public LinkedObjectTemplate<Device>
*/
virtual bool erase_range(uint32_t address, size_t size) = 0;

size_t printTo(Print& p) const;

protected:
PartitionTable mPartitions;
};
Expand Down
3 changes: 3 additions & 0 deletions Sming/Components/Storage/src/include/Storage/Partition.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
****/
#pragma once

#include <Printable.h>
#include <Data/BitSet.h>
#include <Data/CString.h>
#include <memory>
Expand Down Expand Up @@ -370,6 +371,8 @@ class Partition
*/
size_t getBlockSize() const;

size_t printTo(Print& p) const;

protected:
Device* mDevice{nullptr};
const Info* mPart{nullptr};
Expand Down
5 changes: 5 additions & 0 deletions Sming/Components/Storage/src/include/Storage/PartitionTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class PartitionTable
{
}

explicit operator bool() const
{
return mCount != 0;
}

/**
* @name Partition search
* @{
Expand Down
25 changes: 25 additions & 0 deletions Sming/Core/Data/BitSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <limits>
#include <type_traits>
#include <WString.h>
#include <Print.h>

/**
* @brief Manage a set of bit values using enumeration
Expand Down Expand Up @@ -366,6 +367,30 @@ template <typename S, typename E, size_t size_ = sizeof(S) * 8> class BitSet
return any() ? &BitSet::IfHelper : 0;
}

/**
* @brief Class template to print the contents of a BitSet to a String
* @note Requires an implementation of `toString(E)`
*/
size_t printTo(Print& p, const String& separator = ", ") const
{
extern String toString(E e);

size_t n{0};

for(unsigned i = 0; i < size(); ++i) {
auto e = E(i);
if(!test(e)) {
continue;
}
if(n != 0) {
n += p.print(separator);
}
n += p.print(e);
}

return n;
}

private:
void IfHelper() const
{
Expand Down
9 changes: 7 additions & 2 deletions Sming/Core/Data/Range.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ template <typename T> struct TRange {
/**
* @brief Determine if range contains a value
*/
bool contains(T value)
bool contains(T value) const
{
return (value >= min) && (value <= max);
}

/**
* @brief Clip values to within the range
*/
T clip(T value)
T clip(T value) const
{
return (value < min) ? min : (value > max) ? max : value;
}
Expand Down Expand Up @@ -122,6 +122,11 @@ template <typename T> struct TRange {
s += max;
return s;
}

operator String() const
{
return toString();
}
};

template <typename T> inline String toString(TRange<T> range)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,19 @@ void showInfo()
Serial.printf(_F("System Chip ID: %x\r\n"), system_get_chip_id());

int total = 0;
for(auto it = OtaManager.getBootPartitions(); it; ++it) {
debug_d("ROM %s: 0x%08x, SubType: %s", it->name().c_str(), it->address(),
toLongString(it->type(), it->subType()).c_str());
for(auto part : OtaManager.getBootPartitions()) {
Serial.println(part);
total++;
}
debug_d("=======================");
debug_d("Bootable ROMs found: %d", total);
Serial.println(_F("======================="));
Serial << _F("Bootable ROMs found: ") << total << endl;

auto part = OtaManager.getRunningPartition();

Serial.printf(_F("\r\nCurrently running %s: 0x%08x. Application version: %s\r\n"), part.name().c_str(),
part.address(), APP_VERSION);
Serial.println();
Serial << _F("\r\n"
"Currently running ")
<< part.name() << ": 0x" << String(part.address(), HEX) << _F(". Application version: ") << APP_VERSION
<< endl;
}

void connectOk(IpAddress ip, IpAddress mask, IpAddress gateway)
Expand Down
4 changes: 2 additions & 2 deletions Sming/Services/Profiling/MinMax.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Profiling
/**
* @brief Class to track minimum and maximum values of a set of data, with average, total and count
*/
template <typename T> class MinMax : public Printable
template <typename T> class MinMax
{
public:
MinMax(const String& title) : title(title)
Expand Down Expand Up @@ -57,7 +57,7 @@ template <typename T> class MinMax : public Printable
return count;
}

size_t printTo(Print& p) const override;
size_t printTo(Print& p) const;

private:
String title;
Expand Down
Loading

0 comments on commit d21bf5a

Please sign in to comment.