Skip to content

Commit

Permalink
[WIP] Feature: component hosted (#2305)
Browse files Browse the repository at this point in the history
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`.

In the sub-directory ``samples`` inside this component you will find the sample applications that will turn your microcontroller into
a remote 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).
  • Loading branch information
slaff committed Apr 20, 2021
1 parent eaafc21 commit 07ecd3b
Show file tree
Hide file tree
Showing 47 changed files with 1,547 additions and 37 deletions.
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 @@ -263,6 +263,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
12 changes: 2 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ jobs:
addons:
apt:
packages:
- clang-format-6.0
- clang-format-8
- gcc-multilib
- g++-multilib
- g++-9-multilib
- doxygen
- python3-sphinx
- python3-pip
Expand All @@ -28,14 +28,6 @@ jobs:
- jq
env:
- SMING_ARCH=Host

- stage: build
name: C++11
env:
- SMING_ARCH=Esp8266
- SDK_VERSION=3.0.1
- ESP_HOME=$TRAVIS_BUILD_DIR/opt/esp-alt-sdk

- stage: build
name: C++17
env:
Expand Down
24 changes: 24 additions & 0 deletions Sming/Arch/Host/Components/hostlib/include/hostlib/emu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/****
* 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.
*
* emu.h
*
****/

#pragma once

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Executing this function will run once the main emulator loop.
*/
void host_main_loop();

#ifdef __cplusplus
}
#endif
30 changes: 30 additions & 0 deletions Sming/Arch/Host/Components/hostlib/include/hostlib/init.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* hostlib.h
*
* Copyright 2019 mikee47 <mike@sillyhouse.net>
*
* This file is part of the Sming Framework Project
*
* This library is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, version 3 or later.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SHEM.
* If not, see <https://www.gnu.org/licenses/>.
*
****/

#pragma once

#ifdef __cplusplus
extern "C" {
#endif

void host_init();

#ifdef __cplusplus
}
#endif
27 changes: 27 additions & 0 deletions Sming/Arch/Host/Components/hostlib/init.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* init.cpp - Sming Host Emulator startup code
*
* Copyright 2019 mikee47 <mike@sillyhouse.net>
*
* This file is part of the Sming Framework Project
*
* This library is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, version 3 or later.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SHEM.
* If not, see <https://www.gnu.org/licenses/>.
*
****/

#include "include/hostlib/init.h"

extern void init();

void host_init()
{
init();
}
30 changes: 19 additions & 11 deletions Sming/Arch/Host/Components/hostlib/startup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
#include <esp_tasks.h>
#include <host_lwip.h>
#include <stdlib.h>
#include "include/hostlib/init.h"
#include "include/hostlib/emu.h"
#include "include/hostlib/hostlib.h"
#include "include/hostlib/CommandLine.h"
#include <Storage.h>

Expand All @@ -39,8 +42,9 @@

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

Expand Down Expand Up @@ -104,6 +108,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 +245,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 +262,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 */
Expand Down
6 changes: 5 additions & 1 deletion Sming/Arch/Host/Tools/travis/install.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#!/bin/bash
set -ex # exit with nonzero exit code if anything fails

sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-6.0 100
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 60 --slave /usr/bin/g++ g++ /usr/bin/g++-9

sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-8 200
sudo rm -f /usr/local/clang-7.0.0/bin/clang-format

python3 -m pip install -q --upgrade pip

if [ "$BUILD_DOCS" == "1" ]; then
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-Lib-$(CMP_Hosted-Lib_LIBHASH).a $(COMPONENTS_AR)
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
33 changes: 33 additions & 0 deletions Sming/Components/Hosted-Lib/src/Digital.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/****
* 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);
return hostedClient->wait<uint8_t>();
}
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
21 changes: 21 additions & 0 deletions Sming/Components/Hosted/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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_RELINK_VARS += ENABLE_HOSTED
COMPONENT_VARS := ENABLE_HOSTED

ENABLE_HOSTED ?=

ifneq ($(ENABLE_HOSTED),)
COMPONENT_SRCDIRS += $(COMPONENT_PATH)/init/$(ENABLE_HOSTED)
EXTRA_LDFLAGS := -Wl,-wrap,host_init
endif

COMPONENT_VARS += HOSTED_SERVER_IP

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

0 comments on commit 07ecd3b

Please sign in to comment.