[tests] Use TinyConsole::string instead of any

This commit is contained in:
Francois BIOT
2023-02-20 02:24:39 +01:00
parent c21b7b63fb
commit 775fbc14ee
8 changed files with 92 additions and 79 deletions

View File

@@ -3,10 +3,12 @@
#include <assert.h>
#include <map>
#include <unordered_map>
#include "TinyString.h"
#include "TinyConsole.h"
#include <string>
#include <string.h>
using string = TinyConsole::string;
/***
* Allows to store up to 255 different strings with one byte class
* very memory efficient when one string is used many times.
@@ -17,7 +19,7 @@ class StringIndexer
class StringCounter
{
TinyString str;
string str;
uint8_t used=0;
friend class StringIndexer;
@@ -34,9 +36,9 @@ class StringIndexer
public:
using index_t = uint8_t;
static const TinyString& str(const index_t& index)
static const string& str(const index_t& index)
{
static TinyString dummy;
static string dummy;
const auto& it=strings.find(index);
if (it == strings.end()) return dummy;
return it->second.str;
@@ -82,7 +84,7 @@ class StringIndexer
{
if (strings.find(index)==strings.end())
{
strings[index].str = TinyString(str, len);
strings[index].str = string(str, len);
strings[index].used++;
// Serial << "Creating index " << index << " for (" << strings[index].str.c_str() << ") len=" << len << endl;
return index;
@@ -110,7 +112,7 @@ class IndexedString
index=StringIndexer::strToIndex(str, len);
}
IndexedString(const TinyString& str) : IndexedString(str.c_str(), str.length()) {};
IndexedString(const string& str) : IndexedString(str.c_str(), str.length()) {};
~IndexedString() { StringIndexer::release(index); }
@@ -131,7 +133,7 @@ class IndexedString
return i1.index == i2.index;
}
const TinyString& str() const { return StringIndexer::str(index); }
const string& str() const { return StringIndexer::str(index); }
const StringIndexer::index_t& getIndex() const { return index; }

View File

@@ -52,7 +52,7 @@ MqttClient::MqttClient(MqttBroker* local_broker, TcpClient* new_client)
#endif
}
MqttClient::MqttClient(MqttBroker* local_broker, const TinyString& id)
MqttClient::MqttClient(MqttBroker* local_broker, const string& id)
: local_broker(local_broker), clientId(id)
{
alive = 0;
@@ -97,7 +97,7 @@ void MqttClient::connect(MqttBroker* local)
local_broker = local;
}
void MqttClient::connect(TinyString broker, uint16_t port, uint16_t ka)
void MqttClient::connect(string broker, uint16_t port, uint16_t ka)
{
debug("MqttClient::connect_to_host " << broker << ':' << port);
keep_alive = ka;
@@ -128,7 +128,7 @@ void MqttBroker::addClient(MqttClient* client)
clients.push_back(client);
}
void MqttBroker::connect(const TinyString& host, uint16_t port)
void MqttBroker::connect(const string& host, uint16_t port)
{
debug("MqttBroker::connect");
if (remote_broker == nullptr) remote_broker = new MqttClient;
@@ -461,7 +461,7 @@ void MqttClient::processMessage(MqttMessage* mesg)
// ClientId
mesg->getString(payload, len);
clientId = TinyString(payload, len);
clientId = string(payload, len);
payload += len;
if (mqtt_flags & FlagWill) // Will topic
@@ -539,11 +539,11 @@ void MqttClient::processMessage(MqttMessage* mesg)
payload = header+2;
debug("un/subscribe loop");
TinyString qoss;
string qoss;
while(payload < mesg->end())
{
mesg->getString(payload, len); // Topic
debug( " topic (" << TinyString(payload, len) << ')');
debug( " topic (" << string(payload, len) << ')');
// subscribe(Topic(payload, len));
Topic topic(payload, len);
@@ -597,7 +597,7 @@ void MqttClient::processMessage(MqttMessage* mesg)
#if TINY_MQTT_DEBUG
Console << "Received Publish (" << published.str().c_str() << ") size=" << (int)len << endl;
#endif
// << '(' << TinyString(payload, len).c_str() << ')' << " msglen=" << mesg->length() << endl;
// << '(' << string(payload, len).c_str() << ')' << " msglen=" << mesg->length() << endl;
if (qos) payload+=2; // ignore packet identifier if any
len=mesg->end()-payload;
// TODO reset DUP
@@ -889,7 +889,7 @@ void MqttMessage::hexdump(const char* prefix) const
(void)prefix;
#if TINY_MQTT_DEBUG
if (TinyMqtt::debug<2) return;
static std::map<Type, TinyString> tts={
static std::map<Type, string> tts={
{ Connect, "Connect" },
{ ConnAck, "Connack" },
{ Publish, "Publish" },
@@ -902,7 +902,7 @@ void MqttMessage::hexdump(const char* prefix) const
{ PingResp, "Pingresp" },
{ Disconnect, "Disconnect" }
};
TinyString t("Unknown");
string t("Unknown");
Type typ=static_cast<Type>(buffer[0] & 0xF0);
if (tts.find(typ) != tts.end())
t=tts[typ];
@@ -919,7 +919,7 @@ void MqttMessage::hexdump(const char* prefix) const
const char* hex_to_str = " | ";
const char* separator = hex_to_str;
const char* half_sep = " - ";
TinyString ascii;
string ascii;
Console << prefix << " size(" << buffer.size() << "), state=" << state << endl;

View File

@@ -68,13 +68,15 @@ enum __attribute__((packed)) MqttError
MqttInvalidMessage=2,
};
using string = TinyConsole::string;
class Topic : public IndexedString
{
public:
Topic(const TinyString& m) : IndexedString(m){}
Topic(const string& m) : IndexedString(m){}
Topic(const char* s, uint8_t len) : IndexedString(s,len){}
Topic(const char* s) : Topic(s, strlen(s)) {}
// Topic(const TinyString s) : Topic(s.c_str(), s.length()){};
// Topic(const string s) : Topic(s.c_str(), s.length()){};
const char* c_str() const { return str().c_str(); }
@@ -123,7 +125,7 @@ class MqttMessage
void incoming(char byte);
void add(char byte) { incoming(byte); }
void add(const char* p, size_t len, bool addLength=true );
void add(const TinyString& s) { add(s.c_str(), s.length()); }
void add(const string& s) { add(s.c_str(), s.length()); }
void add(const Topic& t) { add(t.str()); }
const char* end() const { return &buffer[0]+buffer.size(); }
const char* getVHeader() const { return &buffer[vheader]; }
@@ -157,7 +159,7 @@ class MqttMessage
private:
void encodeLength();
TinyString buffer;
string buffer;
uint8_t vheader;
uint16_t size; // bytes left to receive
State state;
@@ -182,13 +184,13 @@ class MqttClient
/** Constructor. Broker is the adress of a local broker if not null
If you want to connect elsewhere, leave broker null and use connect() **/
MqttClient(MqttBroker* broker = nullptr, const TinyString& id = TINY_MQTT_DEFAULT_CLIENT_ID);
MqttClient(const TinyString& id) : MqttClient(nullptr, id){}
MqttClient(MqttBroker* broker = nullptr, const string& id = TINY_MQTT_DEFAULT_CLIENT_ID);
MqttClient(const string& id) : MqttClient(nullptr, id){}
~MqttClient();
void connect(MqttBroker* local_broker);
void connect(TinyString broker, uint16_t port, uint16_t keep_alive = 10);
void connect(string broker, uint16_t port, uint16_t keep_alive = 10);
// TODO it seems that connected returns true in tcp mode even if
// no negociation occurred
@@ -203,8 +205,8 @@ class MqttClient
if (tcp_client) tcp_client->write(buf, length);
}
const TinyString& id() const { return clientId; }
void id(const TinyString& new_id) { clientId = new_id; }
const string& id() const { return clientId; }
void id(const string& new_id) { clientId = new_id; }
/** Should be called in main loop() */
void loop();
@@ -222,7 +224,7 @@ class MqttClient
MqttError publish(const Topic&, const char* payload, size_t pay_length);
MqttError publish(const Topic& t, const char* payload) { return publish(t, payload, strlen(payload)); }
MqttError publish(const Topic& t, const String& s) { return publish(t, s.c_str(), s.length()); }
MqttError publish(const Topic& t, const TinyString& s) { return publish(t,s.c_str(),s.length());}
MqttError publish(const Topic& t, const string& s) { return publish(t,s.c_str(),s.length());}
MqttError publish(const Topic& t) { return publish(t, nullptr, 0);};
MqttError subscribe(Topic topic, uint8_t qos=0);
@@ -233,7 +235,7 @@ class MqttClient
// TODO seems to be useless
bool isLocal() const { return tcp_client == nullptr; }
void dump(TinyString indent="")
void dump(string indent="")
{
(void)indent;
#if TINY_MQTT_DEBUG
@@ -299,7 +301,7 @@ class MqttClient
TcpClient* tcp_client=nullptr; // connection to remote broker
std::set<Topic> subscriptions;
TinyString clientId;
string clientId;
CallBack callback = nullptr;
};
@@ -319,12 +321,12 @@ class MqttBroker
void begin() { server->begin(); }
void loop();
void connect(const TinyString& host, uint16_t port=1883);
void connect(const string& host, uint16_t port=1883);
bool connected() const { return state == Connected; }
size_t clientsCount() const { return clients.size(); }
void dump(TinyString indent="")
void dump(string indent="")
{
for(auto client: clients)
client->dump(indent);