Skip to content

Commit

Permalink
Improved embedded HTTP server
Browse files Browse the repository at this point in the history
  • Loading branch information
vanvught committed Jan 10, 2025
1 parent 6bee84a commit 6446927
Show file tree
Hide file tree
Showing 15 changed files with 427 additions and 141 deletions.
10 changes: 10 additions & 0 deletions lib-remoteconfig/.cproject
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lib-display/include}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lib-properties/include}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/include}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lib-flashcodeinstall/include}&quot;"/>
</option>
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="gnu.c.compiler.option.preprocessor.def.symbols.951792494" name="Defined symbols (-D)" superClass="gnu.c.compiler.option.preprocessor.def.symbols" useByScannerDiscovery="false" valueType="definedSymbols">
<listOptionValue builtIn="false" value="ENABLE_TFTP"/>
Expand Down Expand Up @@ -84,6 +85,7 @@
<listOptionValue builtIn="false" value="CONFIG_SHELL_GPS"/>
<listOptionValue builtIn="false" value="ENABLE_NTP_CLIENT"/>
<listOptionValue builtIn="false" value="PHY_TYPE=PHY_GENERIC"/>
<listOptionValue builtIn="false" value="ENABLE_FIRMWARE_UPLOAD"/>
</option>
<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.1810325769" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
</tool>
Expand Down Expand Up @@ -184,6 +186,7 @@
<listOptionValue builtIn="false" value="CONFIG_SHELL_GPS"/>
<listOptionValue builtIn="false" value="ENABLE_NTP_CLIENT"/>
<listOptionValue builtIn="false" value="PHY_TYPE=PHY_GENERIC"/>
<listOptionValue builtIn="false" value="ENABLE_FIRMWARE_UPLOAD"/>
</option>
<inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.1213842804" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/>
</tool>
Expand All @@ -205,6 +208,7 @@
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lib-display/include}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lib-properties/include}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/include}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lib-flashcodeinstall/include}&quot;"/>
</option>
<inputType id="cdt.managedbuild.tool.gnu.assembler.input.1823136615" superClass="cdt.managedbuild.tool.gnu.assembler.input"/>
</tool>
Expand Down Expand Up @@ -333,11 +337,13 @@
<listOptionValue builtIn="false" value="NODE_ARTNET"/>
<listOptionValue builtIn="false" value="NODE_E131"/>
<listOptionValue builtIn="false" value="NODE_NODE"/>
<listOptionValue builtIn="false" value="ENABLE_FIRMWARE_UPLOAD"/>
</option>
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="ilg.gnuarmeclipse.managedbuild.cross.option.assembler.include.paths.1650114521" name="Include paths (-I)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.assembler.include.paths" valueType="includePath">
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/include}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lib-network/include}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lib-hal/include}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lib-flashcodeinstall/include}&quot;"/>
</option>
<inputType id="ilg.gnuarmeclipse.managedbuild.cross.tool.assembler.input.376391692" superClass="ilg.gnuarmeclipse.managedbuild.cross.tool.assembler.input"/>
</tool>
Expand All @@ -357,11 +363,13 @@
<listOptionValue builtIn="false" value="NODE_ARTNET"/>
<listOptionValue builtIn="false" value="NODE_E131"/>
<listOptionValue builtIn="false" value="NODE_NODE"/>
<listOptionValue builtIn="false" value="ENABLE_FIRMWARE_UPLOAD"/>
</option>
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="ilg.gnuarmeclipse.managedbuild.cross.option.c.compiler.include.paths.1916263486" name="Include paths (-I)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.c.compiler.include.paths" useByScannerDiscovery="true" valueType="includePath">
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/include}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lib-network/include}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lib-hal/include}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lib-flashcodeinstall/include}&quot;"/>
</option>
<inputType id="ilg.gnuarmeclipse.managedbuild.cross.tool.c.compiler.input.1946936900" superClass="ilg.gnuarmeclipse.managedbuild.cross.tool.c.compiler.input"/>
</tool>
Expand All @@ -384,6 +392,7 @@
<listOptionValue builtIn="false" value="NODE_ARTNET"/>
<listOptionValue builtIn="false" value="NODE_E131"/>
<listOptionValue builtIn="false" value="NODE_NODE"/>
<listOptionValue builtIn="false" value="ENABLE_FIRMWARE_UPLOAD"/>
</option>
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.include.paths.390972526" name="Include paths (-I)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.include.paths" useByScannerDiscovery="true" valueType="includePath">
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/include}&quot;"/>
Expand All @@ -392,6 +401,7 @@
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lib-ltc/include}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lib-network/include}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/include}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lib-flashcodeinstall/include}&quot;"/>
</option>
<inputType id="ilg.gnuarmeclipse.managedbuild.cross.tool.cpp.compiler.input.269759389" superClass="ilg.gnuarmeclipse.managedbuild.cross.tool.cpp.compiler.input"/>
</tool>
Expand Down
2 changes: 1 addition & 1 deletion lib-remoteconfig/.settings/language.settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</configuration>
<configuration id="ilg.gnuarmeclipse.managedbuild.cross.toolchain.base.309283989.303033930.1102356485.534880544.1965804425" name="H3">
<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="true" env-hash="1396205635181097433" id="org.eclipse.embedcdt.managedbuild.cross.arm.core.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT Arm Cross GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} ${cross_toolchain_flags} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="true" env-hash="1356732499129984059" id="org.eclipse.embedcdt.managedbuild.cross.arm.core.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT Arm Cross GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} ${cross_toolchain_flags} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<language-scope id="org.eclipse.cdt.core.gcc"/>
<language-scope id="org.eclipse.cdt.core.g++"/>
</provider>
Expand Down
2 changes: 1 addition & 1 deletion lib-remoteconfig/Makefile.Linux
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DEFINES =ENABLE_CONTENT
DEFINES =ENABLE_CONTENT ENABLE_FIRMWARE_UPLOAD

#DEFINES+=NDEBUG

Expand Down
49 changes: 46 additions & 3 deletions lib-remoteconfig/include/httpd/httpd.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/**
* @file httpd.h
* @brief HTTP daemon class for managing HTTP server tasks.
*
* This class handles HTTP requests and integrates with the network and mDNS subsystems.
* It uses placement new to construct and destruct request handlers explicitly.
*/
/* Copyright (C) 2021-2024 by Arjan van Vught mailto:[email protected]
/* Copyright (C) 2021-2025 by Arjan van Vught mailto:[email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -35,11 +39,38 @@

#include "../../lib-network/config/net_config.h"

#include "debug.h"

/**
* @class HttpDaemon
* @brief A class that manages an HTTP server with support for multiple connections.
*
* The HttpDaemon class sets up an HTTP server, handles incoming requests, and integrates with mDNS.
*/
class HttpDaemon {
public:
/**
* @brief Constructor for HttpDaemon.
*
* Initializes the HTTP daemon, sets up the TCP listener on port 80,
* creates request handlers, and registers the service with mDNS.
*/
HttpDaemon();

/**
* @brief Destructor for HttpDaemon.
*
* Cleans up resources, unregisters the mDNS service,
* destroys request handlers, and stops the TCP listener.
*/
~HttpDaemon();

/**
* @brief Main loop function to handle HTTP requests.
*
* Reads incoming TCP data, determines the connection handle,
* and dispatches the request to the appropriate handler.
*/
void Run() {
uint32_t nConnectionHandle;
const auto nBytesReceived = Network::Get()->TcpRead(m_nHandle, const_cast<const uint8_t **>(reinterpret_cast<uint8_t **>(&m_pReceiveBuffer)), nConnectionHandle);
Expand All @@ -50,11 +81,23 @@ class HttpDaemon {

DEBUG_PRINTF("nConnectionHandle=%u", nConnectionHandle);

pHandleRequest[nConnectionHandle]->HandleRequest(nBytesReceived, m_pReceiveBuffer);
handleRequest[nConnectionHandle].HandleRequest(nBytesReceived, m_pReceiveBuffer);
}

private:
HttpDeamonHandleRequest *pHandleRequest[TCP_MAX_TCBS_ALLOWED];
/**
* https://www.gd32-dmx.org/memory.html
*/
#if defined (GD32F207RG) || defined (GD32F450VE) || defined (GD32F470ZK)
# define SECTION_HTTPD __attribute__ ((section (".httpd")))
#else
# define SECTION_HTTPD
#endif
/*
* Each handler corresponds to a connection handle. Objects are constructed
* using placement new and must be explicitly destructed.
*/
static inline HttpDeamonHandleRequest handleRequest[TCP_MAX_TCBS_ALLOWED] __attribute__ ((aligned (4))) SECTION_HTTPD;
int32_t m_nHandle { -1 };
char *m_pReceiveBuffer { nullptr };
};
Expand Down
18 changes: 14 additions & 4 deletions lib-remoteconfig/include/httpd/httpdhandlerequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#define HTTPD_HTTPDHANDLEREQUEST_H_

#include <cstdint>
#include <new>

#include "http.h"
#include "net/protocol/tcp.h"
Expand All @@ -42,7 +43,13 @@ static constexpr uint32_t BUFSIZE = HTTPD_CONTENT_SIZE;

class HttpDeamonHandleRequest {
public:
HttpDeamonHandleRequest(uint32_t nConnectionHandle, int32_t nHandle) : m_nConnectionHandle(nConnectionHandle), m_nHandle(nHandle) {
HttpDeamonHandleRequest() : m_nConnectionHandle(0), m_nHandle(-1) {
DEBUG_ENTRY
DEBUG_EXIT

}

HttpDeamonHandleRequest(uint32_t nConnectionHandle, int32_t nHandle) : m_nConnectionHandle(nConnectionHandle), m_nHandle(nHandle) {
DEBUG_ENTRY
DEBUG_PRINTF("[%u] m_nConnectionHandle=%u, m_nHandle=%d", httpd::BUFSIZE, m_nConnectionHandle, m_nHandle);
DEBUG_EXIT
Expand All @@ -58,6 +65,7 @@ class HttpDeamonHandleRequest {
http::Status HandleGetTxt();
http::Status HandlePost(const bool hasDataOnly);
http::Status HandleDelete(const bool hasDataOnly);
http::Status HandlePostJSON();

private:
uint32_t m_nConnectionHandle;
Expand All @@ -69,16 +77,18 @@ class HttpDeamonHandleRequest {

char *m_pUri { nullptr };
char *m_pFileData { nullptr };
const char *m_pContent { nullptr };
char *m_pFirmwareFilename { nullptr };
char *m_pReceiveBuffer { nullptr };
const char *m_pContent { nullptr };

http::Status m_Status { http::Status::UNKNOWN_ERROR };
http::RequestMethod m_RequestMethod { http::RequestMethod::UNKNOWN };
http::contentTypes m_RequestContentType { http::contentTypes::NOT_DEFINED };

bool m_IsAction { false };
bool m_isAction { false };


static char m_DynamicContent[httpd::BUFSIZE];
char m_DynamicContent[httpd::BUFSIZE];
};


Expand Down
24 changes: 1 addition & 23 deletions lib-remoteconfig/include/tftp/tftpfileserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @file tftpfileserver.h
*
*/
/* Copyright (C) 2019-2024 by Arjan van Vught mailto:[email protected]
/* Copyright (C) 2019-2025 by Arjan van Vught mailto:[email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -37,28 +37,6 @@

namespace tftpfileserver {
bool is_valid(const void *pBuffer);
#if defined(__linux__) || defined (__APPLE__)
#else
# if defined (H3)
# if defined(ORANGE_PI)
static constexpr char FILE_NAME[] = "orangepi_zero.uImage";
# else
static constexpr char FILE_NAME[] = "orangepi_one.uImage";
# endif
# elif defined (GD32)
# if defined (GD32F10X)
static constexpr char FILE_NAME[] = "gd32f107.bin";
# elif defined (GD32F20X)
static constexpr char FILE_NAME[] = "gd32f207.bin";
# elif defined (GD32F4XX)
static constexpr char FILE_NAME[] = "gd32f4xx.bin";
# elif defined (GD32H7XX)
static constexpr char FILE_NAME[] = "gd32h7xx.bin";
# else
# error FAMILY is not defined
# endif
# endif
#endif
} // namespace tftpfileserver

class TFTPFileServer final: public TFTPDaemon {
Expand Down
6 changes: 3 additions & 3 deletions lib-remoteconfig/src/h3/shellcmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
*/
/* Copyright (C) 2020 by hippy mailto:[email protected]
* Copyright (C) 2020-2024 by Arjan van Vught mailto:[email protected]
* Copyright (C) 2020-2025 by Arjan van Vught mailto:[email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -31,15 +31,15 @@

#include "debug.h"

void h3_board_dump();

extern "C" {
void h3_board_dump(void);
void h3_dump_memory_mapping(void);
void h3_ccu_pll_dump(void);
void arm_dump_memmap(void);
void arm_dump_page_table(void);
}


namespace shell::dump {
namespace arg {
static constexpr char BOARD[] = "board";
Expand Down
4 changes: 2 additions & 2 deletions lib-remoteconfig/src/h3/tftpfileserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @file tftpfileserver.cpp
*
*/
/* Copyright (C) 2019-2021 by Arjan van Vught mailto:info@orangepi-dmx.nl
/* Copyright (C) 2019-2025 by Arjan van Vught mailto:info@gd32-dmx.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -26,7 +26,7 @@
#include <cstdint>
#include <cassert>

#include "h3/ubootheader.h"
#include "ubootheader.h"
#include "remoteconfig.h"

#include "debug.h"
Expand Down
37 changes: 37 additions & 0 deletions lib-remoteconfig/src/httpd/http_status_messages.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @file http_status_messages.h
*
*/
/* Copyright (C) 2025 by Arjan van Vught mailto:[email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


#ifndef HTTP_STATUS_MESSAGES_H_
#define HTTP_STATUS_MESSAGES_H_

namespace httpd {
static constexpr const char STATUS_400[] = "{\"code\":400,\"status\":\"Bad Request\",\"message\":\"%s\"}\n";
static constexpr auto STATUS_400_LENGTH = sizeof(STATUS_400) - 1;
static constexpr const char STATUS_404[] = "{\"code\":404,\"status\":\"Not found\",\"message\":\"%s\"}\n";
static constexpr auto STATUS_404_LENGTH = sizeof(STATUS_404) - 1;
} // namespace httpd

#endif /* HTTP_STATUS_MESSAGES_H_ */
10 changes: 4 additions & 6 deletions lib-remoteconfig/src/httpd/httpd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @file httpd.cpp
*
*/
/* Copyright (C) 2021-2024 by Arjan van Vught mailto:[email protected]
/* Copyright (C) 2021-2025 by Arjan van Vught mailto:[email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -47,8 +47,7 @@ HttpDaemon::HttpDaemon() {
assert(m_nHandle != -1);

for (uint32_t nIndex = 0; nIndex < TCP_MAX_TCBS_ALLOWED; nIndex++) {
pHandleRequest[nIndex] = new HttpDeamonHandleRequest(nIndex, m_nHandle);
assert(pHandleRequest[nIndex] != nullptr);
new (&handleRequest[nIndex]) HttpDeamonHandleRequest(nIndex, m_nHandle);
}

mdns_service_record_add(nullptr, mdns::Services::HTTP);
Expand All @@ -62,9 +61,8 @@ HttpDaemon::~HttpDaemon() {
mdns_service_record_delete(mdns::Services::HTTP);

for (uint32_t nIndex = 0; nIndex < TCP_MAX_TCBS_ALLOWED; nIndex++) {
if (pHandleRequest[nIndex] != nullptr) {
delete pHandleRequest[nIndex];
}
// Explicitly calling the destructor because objects were constructed with placement new.
handleRequest[nIndex].~HttpDeamonHandleRequest();
}

Network::Get()->TcpEnd(m_nHandle);
Expand Down
Loading

0 comments on commit 6446927

Please sign in to comment.