Skip to content

Commit

Permalink
Improvements to HostEd (#2768)
Browse files Browse the repository at this point in the history
This PR proposes some changes to the Hosted classes

**Update simpleRPC to master**

With clang-tidy #2648 some failures occur in the `simpleRPC` library. Updating this to current master solves the issues.
I've also put all the code into the `simpleRPC` namespace which sorts out the conflict with `Vector` but makes it available for use if required.

**Tidy HostTests module**

Also decode the anonymous 'packet' blob so we can see what's in it and compare with spec. should we wish to update it.

**Use abstract base class for callbacks**

Simplifies implementation since usually all callbacks are required.
Compiler ensures all methods have implementations.
`clang-tidy` gave potential memory leak indication because of Delegates. Not sure if that's real but this fixes it.
  • Loading branch information
mikee47 authored Apr 22, 2024
1 parent c8cffeb commit ea00370
Show file tree
Hide file tree
Showing 25 changed files with 238 additions and 320 deletions.
30 changes: 0 additions & 30 deletions Sming/Arch/Host/Components/hostlib/include/hostlib/init.h

This file was deleted.

27 changes: 0 additions & 27 deletions Sming/Arch/Host/Components/hostlib/init.cpp

This file was deleted.

5 changes: 3 additions & 2 deletions Sming/Arch/Host/Components/hostlib/startup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include <driver/hw_timer.h>
#include <esp_tasks.h>
#include <stdlib.h>
#include "include/hostlib/init.h"
#include "include/hostlib/emu.h"
#include "include/hostlib/hostlib.h"
#include "include/hostlib/CommandLine.h"
Expand All @@ -43,6 +42,8 @@
#include <host_lwip.h>
#endif

extern void init();

namespace
{
static int exitCode;
Expand Down Expand Up @@ -286,7 +287,7 @@ int main(int argc, char* argv[])

System.initialize();

host_init();
init();

while(!done) {
int due = host_main_loop();
Expand Down
4 changes: 3 additions & 1 deletion Sming/Arch/Host/app.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ TARGET_OUT_0 := $(FW_BASE)/$(APP_NAME)$(TOOL_EXT)

# Hosted Settings
ifneq ($(ENABLE_HOSTED),)
COMPONENTS_AR := $(USER_LIBDIR)/$(CLIB_PREFIX)Hosted-Lib-$(CMP_Hosted-Lib_LIBHASH).a $(COMPONENTS_AR)
COMPONENTS_AR := \
$(CMP_Hosted-Lib_TARGETS) \
$(COMPONENTS_AR)
endif

# Target definitions
Expand Down
8 changes: 6 additions & 2 deletions Sming/Components/Hosted/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ HostEd
The hosted component allows Sming's host emulator to run parts of the commands on an actual microcontroller.
The communication is done via `simplePRC <https://simplerpc.readthedocs.io/>`_ and the microcontroller has to be flashed with a special application.


Overview
--------

Sming's host emulator allows easier debugging and development of embedded applications. This component named "Hosted" extends the host emulator
and facilitates testing functionality that only a real microcontroller can provide as for example digital I/O operations or SPI operations.

Expand All @@ -24,7 +26,7 @@ We need to compile and flash also a special application on the desired microcont
This application will act as an RPC Server and will execute the commands from the host emulator on the microcontroller.

In the ``samples`` directory you will find the sample applications that will turn your microcontroller into
an RCP server.
an RPC server.

The compilation and flashing for ESP32, for example, can be done using the following commands::

Expand All @@ -33,7 +35,8 @@ The compilation and flashing for ESP32, for example, can be done using the follo
make flash

If you replace ``SMING_ARCH=Esp32`` with ``SMING_ARCH=Esp8266`` then the hosted application will be compiled and flashed on a ESP8266 microcontroller.
Make sure to replace the values of  WIFI_SSID and WIFI_PWD with the actual name and password for the Access Point (AP).
Make sure to replace the values of WIFI_SSID and WIFI_PWD with the actual name and password for the Access Point (AP).


Communication
-------------
Expand All @@ -42,6 +45,7 @@ can be done using TCP or serial interface.

The ``transport`` classes are located under ``include/Hosted/Transport``.


Configuration
-------------

Expand Down
16 changes: 10 additions & 6 deletions Sming/Components/Hosted/component.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ENABLE_HOSTED ?=

ifneq ($(ENABLE_HOSTED),)
COMPONENT_SRCDIRS += init/$(ENABLE_HOSTED)
EXTRA_LDFLAGS := $(call Wrap,host_init)
EXTRA_LDFLAGS := $(call Wrap,_Z4initv)
COMPONENT_DEPENDS += SerialLib
ifeq ($(ENABLE_HOSTED),tcp)
COMPONENT_DEPENDS += Network
Expand All @@ -20,13 +20,17 @@ ifneq ($(ENABLE_HOSTED),)
endif
endif

COMPONENT_RELINK_VARS += HOSTED_SERVER_IP
COMPONENT_RELINK_VARS += \
HOSTED_SERVER_IP \
HOSTED_COM_PORT \
HOSTED_COM_SPEED

COMPONENT_RELINK_VARS += HOSTED_COM_PORT
HOSTED_COM_PORT ?= $(COM_PORT)

COMPONENT_RELINK_VARS += HOSTED_COM_SPEED
HOSTED_COM_SPEED ?= 115200

COMPONENT_CFLAGS = -DHOSTED_SERVER_IP=$(HOSTED_SERVER_IP) -DHOSTED_COM_PORT="\"$(HOSTED_COM_PORT)"\" -DHOSTED_COM_SPEED=$(HOSTED_COM_SPEED)
COMPONENT_CFLAGS = \
-DHOSTED_SERVER_IP=$(HOSTED_SERVER_IP) \
-DHOSTED_COM_PORT="\"$(HOSTED_COM_PORT)"\" \
-DHOSTED_COM_SPEED=$(HOSTED_COM_SPEED)

COMPONENT_CXXFLAGS := $(COMPONENT_CFLAGS)
29 changes: 11 additions & 18 deletions Sming/Components/Hosted/include/Hosted/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@
#include <hostlib/hostmsg.h>
#include "Util.h"

using namespace simpleRPC;

namespace Hosted
{
constexpr int COMMAND_NOT_FOUND = -1;

class Client
class Client : private simpleRPC::ParserCallbacks
{
public:
using RemoteCommands = HashMap<String, uint8_t>;
Expand Down Expand Up @@ -64,7 +62,7 @@ class Client
return false;
}

rpcPrint(stream, uint8_t(functionId), args...);
simpleRPC::rpcPrint(stream, uint8_t(functionId), args...);
stream.flush();

return true;
Expand Down Expand Up @@ -120,17 +118,12 @@ class Client
{
host_debug_i("Getting remote RPC commands \033[5m...\033[0m");

using namespace simpleRPC;

uint8_t head = 0xff;
stream.write(&head, 1);
char buffer[512];
ParserSettings settings;
settings.startMethods = ParserSettings::SimpleMethod(&Client::startMethods, this);
settings.startMethod = ParserSettings::SimpleMethod(&Client::startMethod, this);
settings.methodSignature = ParserSettings::CharMethod(&Client::methodSignature, this);
settings.methodName = ParserSettings::CharMethod(&Client::methodName, this);
settings.endMethod = ParserSettings::SimpleMethod(&Client::endMethod, this);
settings.endMethods = ParserSettings::SimpleMethod(&Client::endMethods, this);
settings.state = ParserState::ready;
ParserSettings settings{*this};

do {
stream.flush();
Expand Down Expand Up @@ -172,37 +165,37 @@ class Client
String signature;
char methodEndsWith;

void startMethods()
void startMethods() override
{
methodPosition = 0;
commands.clear();
}

void startMethod()
void startMethod() override
{
name = "";
signature = "";
}

void methodSignature(char ch)
void methodSignature(char ch) override
{
signature += ch;
}

void methodName(char ch)
void methodName(char ch) override
{
name += ch;
}

void endMethod()
void endMethod() override
{
if(!commands.contains(name) || signature == ":") {
commands[name] = methodPosition;
}
commands[name + "(" + signature + ")"] = methodPosition++;
}

void endMethods()
void endMethods() override
{
fetchCommands = false;
}
Expand Down
1 change: 0 additions & 1 deletion Sming/Components/Hosted/include/Hosted/Serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ class Serial : public Stream

private:
String ttyDevice;

serialib transport;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
#include <Stream.h>
#include <Delegate.h>

namespace Hosted
{
namespace Transport
namespace Hosted::Transport
{
class BaseTransport
{
Expand All @@ -38,6 +36,4 @@ class BaseTransport
DataHandler handler;
};

} // namespace Transport

} // namespace Hosted
} // namespace Hosted::Transport
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
#include <HardwareSerial.h>
#include "BaseTransport.h"

namespace Hosted
{
namespace Transport
namespace Hosted::Transport
{
class SerialTransport : public BaseTransport
{
Expand All @@ -35,6 +33,4 @@ class SerialTransport : public BaseTransport
}
};

} // namespace Transport

} // namespace Hosted
} // namespace Hosted::Transport
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
#include <Network/TcpServer.h>
#include <Data/Buffer/CircularBuffer.h>

namespace Hosted
{
namespace Transport
namespace Hosted::Transport
{
class TcpClientStream : public Stream
{
public:
TcpClientStream(TcpClient& client, size_t cbufferSize = 1024, size_t threshold = 400)
: cBuffer(cbufferSize), client(client), pendingBytes(0), threshold(threshold)
: cBuffer(cbufferSize), client(client), threshold(threshold)
{
client.setReceiveDelegate(TcpClientDataDelegate(&TcpClientStream::store, this));
}
Expand Down Expand Up @@ -87,7 +85,7 @@ class TcpClientStream : public Stream
private:
CircularBuffer cBuffer;
TcpClient& client;
size_t pendingBytes;
size_t pendingBytes{0};
size_t threshold;

bool store(TcpClient& client, char* data, int size)
Expand All @@ -96,6 +94,4 @@ class TcpClientStream : public Stream
}
};

} // namespace Transport

} // namespace Hosted
} // namespace Hosted::Transport
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@
#include "TcpClientStream.h"
#include <memory>

namespace Hosted
{
namespace Transport
namespace Hosted::Transport
{
class TcpClientTransport : public TcpTransport
{
public:
TcpClientTransport(TcpClient& client) : stream(new TcpClientStream(client))
TcpClientTransport(TcpClient& client) : stream(std::make_unique<TcpClientStream>(client))
{
client.setReceiveDelegate(TcpClientDataDelegate(&TcpClientTransport::process, this));
}
Expand All @@ -44,6 +42,4 @@ class TcpClientTransport : public TcpTransport
std::unique_ptr<TcpClientStream> stream;
};

} // namespace Transport

} // namespace Hosted
} // namespace Hosted::Transport
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@
#include "TcpTransport.h"
#include "TcpClientStream.h"

namespace Hosted
{
namespace Transport
namespace Hosted::Transport
{
class TcpServerTransport : public TcpTransport
{
public:
using ClientMap = ObjectMap<TcpClient*, TcpClientStream>;

TcpServerTransport(TcpServer& server)
{
server.setClientReceiveHandler(TcpClientDataDelegate(&TcpServerTransport::process, this));
Expand All @@ -52,9 +48,9 @@ class TcpServerTransport : public TcpTransport
}

private:
using ClientMap = ObjectMap<TcpClient*, TcpClientStream>;

ClientMap map;
};

} // namespace Transport

} // namespace Hosted
} // namespace Hosted::Transport
Loading

0 comments on commit ea00370

Please sign in to comment.