[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

@@ -15,6 +15,8 @@
#include <string>
#include <map>
using string = TinyString;
bool echo_on = true;
auto green = TinyConsole::green;
auto red = TinyConsole::red;
@@ -32,13 +34,13 @@ const char* password = "";
struct free_broker
{
public:
free_broker(const char* s, uint16_t p, const char* comment) : url(s), port(p) {}
free_broker(const char* s, uint16_t p, const char* /* comment */) : url(s), port(p) {}
std::string url;
string url;
uint16_t port;
};
const std::map<std::string, free_broker> list =
const std::map<string, free_broker> list =
{
{ "mqtthq", { "public.mqtthq.com" , 8083, "publish/subscribe" }},
{ "hivemq", { "broker.hivemq.com", 1883, "" }}
@@ -73,8 +75,8 @@ void onPublish(const MqttClient* srce, const Topic& topic, const char* payload,
}
}
std::map<std::string, MqttClient*> clients;
std::map<std::string, MqttBroker*> brokers;
std::map<string, MqttClient*> clients;
std::map<string, MqttBroker*> brokers;
void setup()
{
@@ -126,12 +128,12 @@ void setup()
if (Console.isTerm()) onCommand("every 333 view");
}
std::string getword(std::string& str, const char* if_empty=nullptr, char sep=' ');
string getword(string& str, const char* if_empty=nullptr, char sep=' ');
int getint(std::string& str, const int if_empty=0)
int getint(string& str, const int if_empty=0)
{
std::string str2=str;
std::string sword = getword(str);
string str2=str;
string sword = getword(str);
if (sword[0] and isdigit(sword[0]))
{
int ret=atoi(sword.c_str());
@@ -143,11 +145,11 @@ int getint(std::string& str, const int if_empty=0)
return if_empty;
}
std::string getword(std::string& str, const char* if_empty/*=nullptr*/, char sep/*=' '*/)
string getword(string& str, const char* if_empty/*=nullptr*/, char sep/*=' '*/)
{
char quote=(str[0]=='"' or str[0]=='\'' ? str[0] : 0);
if (quote) str.erase(0,1);
std::string sword;
string sword;
while(str.length() and (str[0]!=sep or quote))
{
if (str[0]==quote)
@@ -190,7 +192,7 @@ std::string getword(std::string& str, const char* if_empty/*=nullptr*/, char sep
return sword;
}
bool isaddr(std::string s)
bool isaddr(string s)
{
if (s.length()==0 or s.length()>3) return false;
for(char c: s)
@@ -198,14 +200,14 @@ bool isaddr(std::string s)
return true;
}
std::string getip(std::string& str, const char* if_empty=nullptr, char sep=' ')
string getip(string& str, const char* if_empty=nullptr, char sep=' ')
{
std::string addr=getword(str, if_empty, sep);
std::string ip=addr;
std::vector<std::string> build;
string addr=getword(str, if_empty, sep);
string ip=addr;
std::vector<string> build;
while(ip.length())
{
std::string b=getword(ip,nullptr,'.');
string b=getword(ip,nullptr,'.');
if (isaddr(b) && build.size()<4)
{
build.push_back(b);
@@ -219,9 +221,9 @@ std::string getip(std::string& str, const char* if_empty=nullptr, char sep=' ')
{
std::stringstream b;
b << (int)local[3-build.size()];
build.insert(build.begin(), b.str());
build.insert(build.begin(), b.str().c_str());
}
for(std::string s: build)
for(string s: build)
{
if (addr.length()) addr += '.';
addr += s;
@@ -230,22 +232,22 @@ std::string getip(std::string& str, const char* if_empty=nullptr, char sep=' ')
return addr;
}
std::map<std::string, std::string> vars;
std::map<string, string> vars;
std::set<std::string> commands = {
std::set<string> commands = {
"broker", "blink", "client", "connect",
"create", "delete", "debug", "help", "interval",
"list", "ls", "ip", "off", "on", "set",
"publish", "reset", "subscribe", "unsubscribe", "view", "echo", "every"
};
void convertToCommand(std::string& search)
void convertToCommand(string& search)
{
while(search[0]==' ') search.erase(0,1);
if (search.length()==0) return;
std::string matches;
string matches;
int count=0;
for(std::string cmd: commands)
for(string cmd: commands)
{
if (cmd.substr(0, search.length()) == search)
{
@@ -263,7 +265,7 @@ void convertToCommand(std::string& search)
}
}
void replace(const char* d, std::string& str, std::string srch, std::string to)
void replace(const char* d, string& str, string srch, string to)
{
if (d[0] && d[1])
{
@@ -271,7 +273,7 @@ void replace(const char* d, std::string& str, std::string srch, std::string to)
to=d[0]+to+d[1];
size_t pos = 0;
while((pos=str.find(srch, pos)) != std::string::npos)
while((pos=str.find(srch, pos)) != string::npos)
{
str.erase(pos, srch.length());
str.insert(pos, to);
@@ -280,7 +282,7 @@ void replace(const char* d, std::string& str, std::string srch, std::string to)
}
}
void replaceVars(std::string& cmd)
void replaceVars(string& cmd)
{
cmd = ' '+cmd+' ';
@@ -291,12 +293,12 @@ void replaceVars(std::string& cmd)
replace(" .", cmd, it.first, it.second);
replace(" ", cmd, it.first, it.second);
}
cmd.erase(0, cmd.find_first_not_of(" "));
cmd.erase(cmd.find_last_not_of(" ")+1);
cmd.erase(0, cmd.find_first_not_of(' '));
cmd.erase(cmd.find_last_not_of(' ')+1);
}
bool compare(std::string s, const char* cmd)
bool compare(string s, const char* cmd)
{
uint8_t p=0;
while(s[p++]==*cmd++)
@@ -307,11 +309,11 @@ bool compare(std::string s, const char* cmd)
return false;
}
using ClientFunction = void(*)(std::string& cmd, MqttClient* publish);
using ClientFunction = void(*)(string& cmd, MqttClient* publish);
struct Every
{
std::string cmd;
string cmd;
uint32_t ms;
uint32_t next;
uint32_t underrun=0;
@@ -341,19 +343,19 @@ int16_t blink;
std::vector<Every> everies;
void onCommand(const std::string& command)
void onCommand(const string& command)
{
Console << endl;
std::string cmd=command;
string cmd=command;
if (cmd.substr(0,3)!="set") replaceVars(cmd);
eval(cmd);
Console << endl;
Console.prompt();
}
void clientConnect(MqttClient* client, std::string& cmd)
void clientConnect(MqttClient* client, string& cmd)
{
std::string remote = getword(cmd);
string remote = getword(cmd);
uint16_t port;
auto it=list.find(remote);
if (it != list.end())
@@ -369,18 +371,18 @@ void clientConnect(MqttClient* client, std::string& cmd)
Console << (client->connected() ? "connected." : "not connected") << endl;
}
void eval(std::string& cmd)
void eval(string& cmd)
{
while(cmd.length())
{
MqttError retval = MqttOk;
std::string s;
string s;
MqttBroker* broker = nullptr;
MqttClient* client = nullptr;
// client.function notation
if (cmd.find('.') != std::string::npos &&
if (cmd.find('.') != string::npos &&
cmd.find('.') < cmd.find(' '))
{
s=getword(cmd, nullptr, '.');
@@ -632,7 +634,7 @@ void eval(std::string& cmd)
}
else if (compare(s, "broker"))
{
std::string id=getword(cmd);
string id=getword(cmd);
if (clients.find(id) != clients.end())
{
Console << "A client already have that name" << endl;
@@ -663,7 +665,7 @@ void eval(std::string& cmd)
}
else if (compare(s, "client"))
{
std::string id=getword(cmd);
string id=getword(cmd);
if (brokers.find(id) != brokers.end())
{
Console << "A broker have that name" << endl;
@@ -700,7 +702,7 @@ void eval(std::string& cmd)
}
else if (compare(s, "set"))
{
std::string name(getword(cmd));
string name(getword(cmd));
if (name.length()==0)
{
for(auto it: vars)
@@ -812,7 +814,7 @@ void loop()
if (not every.active) continue;
if (every.ms && every.cmd.length() && ms > every.next)
{
std::string cmd(every.cmd);
string cmd(every.cmd);
eval(cmd);
every.next += every.ms;
if (ms > every.next and ms > every.underrun)
@@ -845,7 +847,6 @@ void loop()
out++;
}
static long count;
#if defined(ESP9266)
MDNS.update();
#endif

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);

View File

@@ -12,9 +12,11 @@
* Also, this will allow to mock and thus run Action on github
**/
using string = TinyConsole::string;
MqttBroker broker(1883);
std::map<TinyString, std::map<Topic, int>> published; // map[client_id] => map[topic] = count
std::map<string, std::map<Topic, int>> published; // map[client_id] => map[topic] = count
const char* lastPayload;
size_t lastLength;

View File

@@ -16,6 +16,8 @@
* Checks with a local broker. Clients must connect to the local broker
**/
using string = TinyConsole::string;
// if ascii_pos = 0, no ascii dump, else ascii dump starts after column ascii_pos
std::string bufferToHexa(const uint8_t* buffer, size_t length, char sep = 0, size_t ascii_pos = 0)
{
@@ -78,7 +80,7 @@ String toString(const IPAddress& ip)
MqttBroker broker(1883);
std::map<TinyString, std::map<Topic, int>> published; // map[client_id] => map[topic] = count
std::map<string, std::map<Topic, int>> published; // map[client_id] => map[topic] = count
char* lastPayload = nullptr;
size_t lastLength;

View File

@@ -11,9 +11,11 @@
* Checks with a local broker. Clients must connect to the local broker
**/
using string = TinyConsole::string;
MqttBroker broker(1883);
std::map<TinyString, std::map<Topic, int>> published; // map[client_id] => map[topic] = count
std::map<string, std::map<Topic, int>> published; // map[client_id] => map[topic] = count
char* lastPayload = nullptr;
size_t lastLength;

View File

@@ -9,6 +9,8 @@
*
**/
using string = TinyConsole::string;
test(indexer_empty)
{
assertEqual(StringIndexer::count(), 0);
@@ -82,7 +84,7 @@ test(indexer_indexed_operator_eq)
test(indexer_get_string)
{
TinyString sone("one");
string sone("one");
IndexedString one(sone);
assertTrue(sone==one.str());