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

[WIP] Feature: component hosted #2093

Closed
wants to merge 10 commits into from
30 changes: 20 additions & 10 deletions Sming/Arch/Host/Components/hostlib/startup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,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 +111,16 @@ 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 +247,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 +264,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
7 changes: 6 additions & 1 deletion Sming/Arch/Host/Components/sming-arch/component.mk
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ COMPONENT_DEPENDS := \
lwip \
spi_flash \
vflash \
rboot
rboot \


ifneq ($(ENABLE_HOSTED),)
COMPONENT_DEPENDS += Hosted-Lib Hosted-Transport
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
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.a $(USER_LIBDIR)/$(CLIB_PREFIX)Hosted-Lib.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 := src
COMPONENT_DEPENDS := Hosted
34 changes: 34 additions & 0 deletions Sming/Components/Hosted-Lib/src/DigitalHosted.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <Digital.h>
#include <HostedClient.h>

extern HostedClient* hostedClient;

void pinMode(uint16_t pin, uint8_t mode)
{
NEW_HD_COMMAND(message, PinMode, {
command->pin = pin;
command->mode = (PinMode)mode;
});

hostedClient->send(&message);
}

void digitalWrite(uint16_t pin, uint8_t val)
{
NEW_HD_COMMAND(message, DigitalWrite, {
command->pin = pin;
command->value = val;
});

hostedClient->send(&message);
}

uint8_t digitalRead(uint16_t pin)
{
NEW_HD_COMMAND(message, DigitalRead, { command->pin = pin; });

hostedClient->send(&message);
HostedCommand response = hostedClient->wait();

return uint8_t(response.payload.responseDigitalRead.value);
}
60 changes: 60 additions & 0 deletions Sming/Components/Hosted-Lib/src/SpiHosted.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <SPI.h>
#include <HostedClient.h>
#include <Data/Stream/MemoryDataStream.h>

extern HostedClient* hostedClient;

// define the static singleton
SPIClass SPI;

bool SPIClass::begin()
{
NEW_HD_COMMAND(message, SpiBeginTransaction, { command->has_settings = 0; });

return hostedClient->send(&message);
}

void SPIClass::prepare(SPISettings& mySettings)
{
NEW_HD_COMMAND(message, SpiBeginTransaction, {
command->has_settings = 1;
// command->settings.speed = mySettings.speed;
command->settings.byteOrder = SpiSettings_ByteOrder(mySettings.byteOrder);
command->settings.dataMode = SpiSettings_DataMode(mySettings.dataMode);
});

hostedClient->send(&message);
}

/** @brief transfer(uint8_t *buffer, size_t numberBytes)
* @param buffer in/out
* @param numberBytes length of buffer
*
* SPI transfer is based on a simultaneous send and receive:
* The buffered transfers does split up the conversation internaly into 64 byte blocks.
* The received data is stored in the buffer passed by reference.
* (the data past in is replaced with the data received).
*
* SPI.transfer(buffer, size) : memory buffer of length size
*/
void SPIClass::transfer(uint8_t* buffer, size_t numberBytes)
{
PbData data{buffer, numberBytes};

NEW_HD_COMMAND(message, SpiTransfer, {
command->data.arg = &data;
command->data.funcs.encode = &pbEncodeData;
});

hostedClient->send(&message);
HostedCommand response = hostedClient->wait();

auto resultData = static_cast<MemoryDataStream*>(response.payload.responseSpiTransfer.data.arg);
if(resultData == nullptr) {
memset(buffer, 0, numberBytes);
return;
}

resultData->readBytes(reinterpret_cast<char*>(buffer), numberBytes);
delete resultData;
}
Empty file.
8 changes: 8 additions & 0 deletions Sming/Components/Hosted-Transport/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
COMPONENT_DEPENDS := Hosted
ifneq ($(ENABLE_HOSTED),)
COMPONENT_SRCDIRS += src/$(ENABLE_HOSTED)
endif

COMPONENT_VARS := ENABLE_HOSTED HOSTED_SERVER_IP
COMPONENT_CFLAGS := -DHOSTED_SERVER_IP=$(HOSTED_SERVER_IP)
COMPONENT_CXXFLAGS := $(COMPONENT_CFLAGS)
63 changes: 63 additions & 0 deletions Sming/Components/Hosted-Transport/src/tcp/HostedTcpStream.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <SmingCore.h>
#include <Data/Buffer/CircularBuffer.h>

class HostedTcpStream : public ReadWriteStream
{
public:
HostedTcpStream(const String& host, uint16_t port) : buffer(1024), host(host), port(port)
{
auto onCompleted = [](TcpClient& client, bool successful) {
// onCompleted;
};

auto onReadyToSend = [](TcpClient& client, TcpConnectionEvent sourceEvent) {

};

auto onReceive = [this](TcpClient& client, char* data, int size) -> bool {
size_t written = buffer.write(reinterpret_cast<const uint8_t*>(data), size);
return written == size_t(size);
};

client = new TcpClient(onCompleted, onReadyToSend, onReceive);
}

size_t write(const uint8_t* buffer, size_t size) override
{
if(!client->isProcessing()) {
client->connect(host, int(port));
}

if(!client->send(reinterpret_cast<const char*>(buffer), size)) {
return 0;
}

return size;
}

uint16_t readMemoryBlock(char* data, int bufSize) override
{
return buffer.readMemoryBlock(data, bufSize);
}

bool seek(int len) override
{
return buffer.seek(len);
}

bool isFinished() override
{
return false;
}

void flush() override
{
client->commit();
}

private:
TcpClient* client{nullptr};
CircularBuffer buffer;
String host;
uint16_t port{0};
};
39 changes: 39 additions & 0 deletions Sming/Components/Hosted-Transport/src/tcp/InitClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <SmingCore.h>
#include <HostedClient.h>
#include "HostedTcpStream.h"

HostedClient* hostedClient{nullptr};

#ifndef WIFI_SSID
#define WIFI_SSID "PleaseEnterSSID" // Put your SSID and password here
#define WIFI_PWD "PleaseEnterPass"
#endif

#ifndef HOSTED_SERVER_IP
#define REMOTE_IP IpAddress("192.168.13.1")
#else
#define STRINGIFY_HELPER(X) #X
#define STRINGIFY(X) STRINGIFY_HELPER(X)
#define REMOTE_IP STRINGIFY(HOSTED_SERVER_IP)
#endif

extern void init();

static void ready(IpAddress ip, IpAddress mask, IpAddress gateway)
{
IpAddress remoteIp(REMOTE_IP);
if(gateway == IpAddress("192.168.4.1")) {
remoteIp = gateway;
}

hostedClient = new HostedClient(new HostedTcpStream(remoteIp.toString(), 4031));
init();
}

void host_init()
{
WifiEvents.onStationGotIP(ready);
WifiStation.enable(true);
WifiStation.config(_F(WIFI_SSID), _F(WIFI_PWD));
WifiStation.connect();
}
Empty file added Sming/Components/Hosted/.cs
Empty file.
Loading