Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: component hosted #2305

Merged
merged 28 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
304c805
Drafting ideas about app on host and some parts on the device.
slav-at-attachix Jun 29, 2020
7334e0b
further research ...
slav-at-attachix Jul 2, 2020
60a6960
And we have a running minimal HostedServer using Tcp transport.
slav-at-attachix Jul 2, 2020
b0bd00a
Replaced nanopb code with submodule.
slav-at-attachix Jul 3, 2020
3fe8f77
Ready with the initial version of Hosted.
slav-at-attachix Jul 9, 2020
5aa9226
Added initial documentation.
slav-at-attachix Jul 13, 2020
27633cb
Initial work on SPI support.
slav-at-attachix Aug 7, 2020
711ddc3
Feature/component hosted review (#68)
mikee47 Feb 14, 2021
48abae7
Removed Components/nanopb. Will be using Libraries/nanopb.
slav-at-attachix Feb 18, 2021
4093b3f
Trying to compile the code with the latest develop version.
slav-at-attachix Feb 18, 2021
9658b9e
Initial code. Nothing to see yet.
slav-at-attachix Apr 8, 2021
c17a553
Untested changes.
slav-at-attachix Apr 8, 2021
6ce358f
Some progress ....
slav-at-attachix Apr 9, 2021
c4755f3
PoC is ready.
slav-at-attachix Apr 10, 2021
49013b8
Initial version of the SimpleRPC stream parser for the headers.
slav-at-attachix Apr 11, 2021
9d56171
Fixes.
slav-at-attachix Apr 14, 2021
b9115e5
Added Hosted tests.
slav-at-attachix Apr 15, 2021
a465ba1
Headers and coding style.
slav-at-attachix Apr 16, 2021
e0a2128
Travis is up and clang-format has to be updated.
slav-at-attachix Apr 16, 2021
b51536a
Run Hosted Tcp tests only if network is present.
slav-at-attachix Apr 16, 2021
d67b771
Update Travis to use GCC 9.
slav-at-attachix Apr 16, 2021
be59a41
Final touches for the initial and not reviewed yet version.
slav-at-attachix Apr 16, 2021
cbb1f81
Applied recommendations from mikee.
slav-at-attachix Apr 16, 2021
583b640
Applying review recommendations.
slav-at-attachix Apr 18, 2021
563f25a
Decrease lag when using TCP.
slav-at-attachix Apr 18, 2021
54882dc
Moving the main_loop function to a separate header. Should fix issue …
slav-at-attachix Apr 19, 2021
24ec69e
Replace weak function with function wrapper.
slav-at-attachix Apr 19, 2021
369c483
Use only Esp8266 EQT for Travis builds.
slav-at-attachix Apr 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
[submodule "Libraries.flatbuffers"]
path = Sming/Libraries/flatbuffers/src
url = https://github.com/google/flatbuffers.git
ignore = dirty
ignore = dirty
[submodule "Libraries.GoogleCast"]
path = Sming/Libraries/GoogleCast
url = https://github.com/slaff/Sming-GoogleCast.git
Expand Down Expand Up @@ -259,6 +259,10 @@
path = Sming/Libraries/SignalGenerator
url = https://github.com/mikee47/SignalGenerator
ignore = dirty
[submodule "Libraries.simpleRPC"]
path = Sming/Libraries/simpleRPC/simpleRPC
url = https://github.com/jfjlaros/simpleRPC.git
ignore = dirty
[submodule "Libraries.SmingTest"]
path = Sming/Libraries/SmingTest
url = https://github.com/mikee47/SmingTest
Expand Down
3 changes: 3 additions & 0 deletions Sming/Arch/Host/Components/hostlib/include/hostlib/hostlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ int msleep(unsigned ms);
*/
size_t getHostAppDir(char* path, size_t bufSize);


void host_main_loop();

#ifdef __cplusplus
}
#endif
32 changes: 22 additions & 10 deletions Sming/Arch/Host/Components/hostlib/startup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <esp_tasks.h>
#include <host_lwip.h>
#include <stdlib.h>
#include "include/hostlib/hostlib.h"
#include "include/hostlib/CommandLine.h"
#include <Storage.h>

Expand All @@ -39,11 +40,18 @@

static int exitCode = 0;
static bool done = false;
static bool lwip_initialised = false;
static OneShotElapseTimer<NanoTime::Milliseconds> lwipServiceTimer;

extern void init();
extern void host_wifi_lwip_init_complete();
extern void host_init_bootloader();

void __attribute__((weak)) host_init()
{
init();
}

static void cleanup()
{
hw_timer_cleanup();
Expand Down Expand Up @@ -104,6 +112,17 @@ static void pause(int secs)
}
}

void host_main_loop()
{
host_service_tasks();
host_service_timers();
if (lwip_initialised && lwipServiceTimer.expired()) {
host_lwip_service();
lwipServiceTimer.start();
}
system_soft_wdt_feed();
}

int main(int argc, char* argv[])
{
trap_exceptions();
Expand Down Expand Up @@ -230,7 +249,7 @@ int main(int argc, char* argv[])
sockets_initialise();
CUartServer::startup(config.uart);

bool lwip_initialised = false;

if(config.enable_network) {
lwip_initialised = host_lwip_init(&config.lwip);
if(lwip_initialised) {
Expand All @@ -247,18 +266,11 @@ int main(int argc, char* argv[])

System.initialize();

init();
host_init();

OneShotElapseTimer<NanoTime::Milliseconds> lwipServiceTimer;
lwipServiceTimer.reset<LWIP_SERVICE_INTERVAL>();
while(!done) {
host_service_tasks();
host_service_timers();
if(lwip_initialised && lwipServiceTimer.expired()) {
host_lwip_service();
lwipServiceTimer.start();
}
system_soft_wdt_feed();
host_main_loop();
}

host_debug_i(">> Normal Exit <<\n");
Expand Down
5 changes: 5 additions & 0 deletions Sming/Arch/Host/Components/sming-arch/component.mk
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ COMPONENT_DEPENDS := \
spi_flash \
vflash \
rboot


ifneq ($(ENABLE_HOSTED),)
COMPONENT_DEPENDS += Hosted-Lib
endif

# => Platform WiFi
COMPONENT_VARS := \
Expand Down
14 changes: 14 additions & 0 deletions Sming/Arch/Host/Core/SPI.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "SPI.h"

bool SPIClass::begin()
{
return false;
}

void SPIClass::transfer(uint8_t* buffer, size_t numberBytes)
{
}

void SPIClass::prepare(SPISettings& settings)
{
}
14 changes: 4 additions & 10 deletions Sming/Arch/Host/Core/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,18 @@
class SPIClass : public SPIBase
{
public:
bool begin() override
{
return false;
}
bool begin() override;

void end() override
{
}

using SPIBase::beginTransaction;
using SPIBase::transfer;
void transfer(uint8_t* buffer, size_t numberBytes) override
{
}
void transfer(uint8_t* buffer, size_t numberBytes) override;

protected:
void prepare(SPISettings& settings) override
{
}
void prepare(SPISettings& settings) override;
};

/** @brief Global instance of SPI class */
Comment on lines 30 to 47
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've added SPI.cpp but not made any changes to the code. Revert SPI.cpp and SPI.h.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have split them into header and implementation to make it easier for the hosted component to replace the real implementation with its own implementation. I would prefer to leave it as it is and add the SPI hosted implementation in a separate PR if that is ok for you.

Expand Down
5 changes: 5 additions & 0 deletions Sming/Arch/Host/app.mk
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ LDFLAGS += \
# Executable
TARGET_OUT_0 := $(FW_BASE)/$(APP_NAME)$(TOOL_EXT)

# Hosted Settings
ifneq ($(ENABLE_HOSTED),)
COMPONENTS_AR := $(USER_LIBDIR)/$(CLIB_PREFIX)Hosted-$(CMP_Hosted_LIBHASH).a $(USER_LIBDIR)/$(CLIB_PREFIX)Hosted-Lib-$(CMP_Hosted-Lib_LIBHASH).a $(COMPONENTS_AR)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikee47 Is it possible with the existing build system for a component to put its library at the front of the list of compiled libraries?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably the only way. I'm guessing the reason for this is to get the linkage with weak functions working properly? Note that weak functions won't work at all in Windows! Function wrappers are more reliable - the malloc_count Component uses these a lot so a good reference.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function wrappers are more reliable - the malloc_count Component uses these a lot so a good reference.

Ok, thanks for pointing this out. The host_init weak function is replaced with wrapper.

endif

# Target definitions

.PHONY: application
Expand Down
Empty file added Sming/Components/Hosted-Lib/.cs
Empty file.
2 changes: 2 additions & 0 deletions Sming/Components/Hosted-Lib/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
COMPONENT_SRCDIRS := $(COMPONENT_PATH)/src
COMPONENT_DEPENDS := Hosted
35 changes: 35 additions & 0 deletions Sming/Components/Hosted-Lib/src/Digital.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/****
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
* Created 2015 by Skurydin Alexey
* http://github.com/SmingHub/Sming
* All files of the Sming Core are provided under the LGPL v3 license.
*
* Digital.cpp
*
* @author 2021 Slavey Karadzhov <slav@attachix.com>
*
*
****/

#include <Digital.h>
#include <Hosted/Client.h>

extern Hosted::Client* hostedClient;

void pinMode(uint16_t pin, uint8_t mode)
{
hostedClient->send(__func__, pin, mode);
}

void digitalWrite(uint16_t pin, uint8_t val)
{
hostedClient->send(__func__, pin, val);
}

uint8_t digitalRead(uint16_t pin)
{
hostedClient->send(__func__, pin);
uint8_t response = hostedClient->wait<uint8_t>();

return response;
}
Empty file added Sming/Components/Hosted/.cs
Empty file.
52 changes: 52 additions & 0 deletions Sming/Components/Hosted/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
HostEd
======

.. highlight:: bash

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.

For example in order to run the Basic_Blink application under the host emulator and run the actual blinking of a LED on a microcontroller
we can compile the application using the following directives::

make SMING_ARCH=Host ENABLE_HOSTED=tcp HOSTED_SERVER_IP=192.168.4.1

`SMING_ARCH=Host` instructs the build system to build the application for the Host architecture.
`ENABLE_HOSTED=tcp` instructs the host emulator to communication with the real microcontroller using TCP
`HOSTED_SERVER_IP=192.168.4.1` instructs the host emulator to connect to IP `192.168.4.1`.

We need to compile and flash also a special application on the desired microcontroller.
This application will act as an RPC Server and will execute the commands from the host emulator on the microcontroller.

In the sub-directory ``samples`` inside this directory you will find the sample applications that will turn your microcontroller into
RCP server.

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

cd samples/tcp
make SMING_ARCH=Esp32 WIFI_SSID=YourSSID WIFI_PWD=YourPassword
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).

Communication
-------------
At the moment the communication between an application running on the Host and the RCP server running on a microcontroller
can be done using TCP or serial interface.
The ``transport`` classes are located under ``include/Hosted/Transport``.

Configuration
-------------
.. envvar:: ENABLE_HOSTED

Default: empty (disabled)

Enables the hosted component. Valid values for the moment are:
- tcp - for communication over TCP network.
- serial - for communication over serial interface
19 changes: 19 additions & 0 deletions Sming/Components/Hosted/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
COMPONENT_SRCDIRS := $(COMPONENT_PATH)/src
COMPONENT_INCDIRS := $(COMPONENT_SRCDIRS) $(COMPONENT_PATH)/include
COMPONENT_DEPENDS := simpleRPC

# Architecture of the device where the hosted service will be flashed
HOSTED_ARCH ?= Esp8266

COMPONENT_VARS := ENABLE_HOSTED

ENABLE_HOSTED ?=

ifneq ($(ENABLE_HOSTED),)
COMPONENT_SRCDIRS += $(COMPONENT_PATH)/init/$(ENABLE_HOSTED)
endif

COMPONENT_VARS += HOSTED_SERVER_IP

COMPONENT_CFLAGS = -DHOSTED_SERVER_IP=$(HOSTED_SERVER_IP)
COMPONENT_CXXFLAGS := $(COMPONENT_CFLAGS)
Loading