Skip to content

Commit

Permalink
built-in FW-Mngmt over FTP (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
matth-x committed Jun 11, 2024
1 parent e1b4d22 commit 5956d12
Show file tree
Hide file tree
Showing 19 changed files with 1,335 additions and 140 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ lib
mo_store
src/ArduinoJson*
src/main.cpp
tests/helpers/ArduinoJson*
coverage.info
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
### Added

- Provide ChargePointStatus in API ([#309](https://github.com/matth-x/MicroOcpp/pull/309))
- Built-in OTA over FTP ([#313](https://github.com/matth-x/MicroOcpp/pull/313))
- Built-in Diagnostics over FTP ([#313](https://github.com/matth-x/MicroOcpp/pull/313))

### Removed

- ESP32 built-in HTTP OTA ([#313](https://github.com/matth-x/MicroOcpp/pull/313))

### Fixed

- Skip Unix files . and .. in ftw_root ([#313](https://github.com/matth-x/MicroOcpp/pull/313))

## [1.1.0] - 2024-05-21

Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ set(MO_SRC_UNIT
tests/Variables.cpp
tests/Transactions.cpp
tests/Certificates.cpp
tests/FirmwareManagement.cpp
)

add_executable(mo_unit_tests
Expand Down
32 changes: 26 additions & 6 deletions src/MicroOcpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <MicroOcpp/Core/OperationRegistry.h>
#include <MicroOcpp/Core/FilesystemAdapter.h>
#include <MicroOcpp/Core/FilesystemUtils.h>
#include <MicroOcpp/Core/Ftp.h>
#include <MicroOcpp/Core/FtpMbedTLS.h>

#include <MicroOcpp/Operations/Authorize.h>
#include <MicroOcpp/Operations/StartTransaction.h>
Expand Down Expand Up @@ -272,6 +274,11 @@ void mocpp_initialize(Connection& connection, const char *bootNotificationCreden
configuration_init(filesystem); //call before each other library call

context = new Context(connection, filesystem, bootstats.bootNr, version);

#if MO_ENABLE_MBEDTLS
context->setFtpClient(makeFtpClientMbedTLS());
#endif //MO_ENABLE_MBEDTLS

auto& model = context->getModel();

model.setTransactionStore(std::unique_ptr<TransactionStore>(
Expand Down Expand Up @@ -328,12 +335,25 @@ void mocpp_initialize(Connection& connection, const char *bootNotificationCreden
new ResetService(*context)));
}

#if MO_PLATFORM == MO_PLATFORM_ARDUINO && !defined(MO_CUSTOM_UPDATER)
#if defined(ESP32) || defined(ESP8266)
model.setFirmwareService(std::unique_ptr<FirmwareService>(
makeDefaultFirmwareService(*context))); //instantiate FW service + ESP installation routine
#endif //defined(ESP32) || defined(ESP8266)
#endif //MO_PLATFORM == MO_PLATFORM_ARDUINO && !defined(MO_CUSTOM_UPDATER)
#if !defined(MO_CUSTOM_UPDATER)
#if MO_PLATFORM == MO_PLATFORM_ARDUINO && defined(ESP32) && MO_ENABLE_MBEDTLS
model.setFirmwareService(
makeDefaultFirmwareService(*context)); //instantiate FW service + ESP installation routine
#elif MO_PLATFORM == MO_PLATFORM_ARDUINO && defined(ESP8266)
model.setFirmwareService(
makeDefaultFirmwareService(*context)); //instantiate FW service + ESP installation routine
#endif //MO_PLATFORM
#endif //!defined(MO_CUSTOM_UPDATER)

#if !defined(MO_CUSTOM_DIAGNOSTICS)
#if MO_PLATFORM == MO_PLATFORM_ARDUINO && defined(ESP32) && MO_ENABLE_MBEDTLS
model.setDiagnosticsService(
makeDefaultDiagnosticsService(*context, filesystem)); //instantiate Diag service + ESP hardware diagnostics
#elif MO_ENABLE_MBEDTLS
model.setDiagnosticsService(
makeDefaultDiagnosticsService(*context, filesystem)); //instantiate Diag service
#endif //MO_PLATFORM
#endif //!defined(MO_CUSTOM_DIAGNOSTICS)

#if MO_PLATFORM == MO_PLATFORM_ARDUINO && (defined(ESP32) || defined(ESP8266))
setOnResetExecute(makeDefaultResetFn());
Expand Down
8 changes: 8 additions & 0 deletions src/MicroOcpp/Core/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,11 @@ const ProtocolVersion& Context::getVersion() {
Connection& Context::getConnection() {
return connection;
}

void Context::setFtpClient(std::unique_ptr<FtpClient> ftpClient) {
this->ftpClient = std::move(ftpClient);
}

FtpClient *Context::getFtpClient() {
return ftpClient.get();
}
6 changes: 6 additions & 0 deletions src/MicroOcpp/Core/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <MicroOcpp/Core/OperationRegistry.h>
#include <MicroOcpp/Core/RequestQueue.h>
#include <MicroOcpp/Core/Ftp.h>
#include <MicroOcpp/Model/Model.h>
#include <MicroOcpp/Version.h>

Expand All @@ -26,6 +27,8 @@ class Context {

std::unique_ptr<RequestQueue> preBootQueue;

std::unique_ptr<FtpClient> ftpClient;

public:
Context(Connection& connection, std::shared_ptr<FilesystemAdapter> filesystem, uint16_t bootNr, ProtocolVersion version);
~Context();
Expand All @@ -46,6 +49,9 @@ class Context {
const ProtocolVersion& getVersion();

Connection& getConnection();

void setFtpClient(std::unique_ptr<FtpClient> ftpClient);
FtpClient *getFtpClient();
};

} //end namespace MicroOcpp
Expand Down
3 changes: 3 additions & 0 deletions src/MicroOcpp/Core/FilesystemAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,9 @@ class PosixFilesystemAdapter : public FilesystemAdapter {

int err = 0;
while (auto entry = readdir(dir)) {
if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
continue; //files . and .. are specific to desktop systems and rarely appear on microcontroller filesystems. Filter them
}
err = fn(entry->d_name);
if (err) {
break;
Expand Down
2 changes: 1 addition & 1 deletion src/MicroOcpp/Core/FilesystemUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ bool FilesystemUtils::storeJson(std::shared_ptr<FilesystemAdapter> filesystem, c

bool FilesystemUtils::remove_if(std::shared_ptr<FilesystemAdapter> filesystem, std::function<bool(const char*)> pred) {
auto ret = filesystem->ftw_root([filesystem, pred] (const char *fpath) {
if (pred(fpath) && fpath[0] != '.') {
if (pred(fpath)) {

char fn [MO_MAX_PATH_SIZE] = {'\0'};
auto ret = snprintf(fn, MO_MAX_PATH_SIZE, MO_FILENAME_PREFIX "%s", fpath);
Expand Down
21 changes: 17 additions & 4 deletions src/MicroOcpp/Core/Ftp.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,24 @@
extern "C" {
#endif

typedef enum {
MO_FtpCloseReason_Undefined,
MO_FtpCloseReason_Success,
MO_FtpCloseReason_Failure
} MO_FtpCloseReason;

typedef struct ocpp_ftp_download {
void *user_data; //set this at your choice. MO passes it back to the functions below

void (*loop)(void *user_data);
void (*is_active)(void *user_data);
} ocpp_ftp_download;

typedef struct ocpp_ftp_upload {
void *user_data; //set this at your choice. MO passes it back to the functions below

void (*loop)(void *user_data);
void (*is_active)(void *user_data);
} ocpp_ftp_upload;

typedef struct ocpp_ftp_client {
Expand All @@ -31,7 +39,7 @@ typedef struct ocpp_ftp_client {
ocpp_ftp_download* (*get_file)(void *user_data,
const char *ftp_url, // ftp[s]://[user[:pass]@]host[:port][/directory]/filename
size_t (*file_writer)(void *mo_data, unsigned char *data, size_t len),
void (*on_close)(void *mo_data),
void (*on_close)(void *mo_data, MO_FtpCloseReason reason),
void *mo_data,
const char *ca_cert); // nullptr to disable cert check; will be ignored for non-TLS connections

Expand All @@ -40,7 +48,7 @@ typedef struct ocpp_ftp_client {
ocpp_ftp_upload* (*post_file)(void *user_data,
const char *ftp_url, // ftp[s]://[user[:pass]@]host[:port][/directory]/filename
size_t (*file_reader)(void *mo_data, unsigned char *buf, size_t bufsize),
void (*on_close)(void *mo_data),
void (*on_close)(void *mo_data, MO_FtpCloseReason reason),
void *mo_data,
const char *ca_cert); // nullptr to disable cert check; will be ignored for non-TLS connections

Expand All @@ -57,27 +65,32 @@ namespace MicroOcpp {

class FtpDownload {
public:
virtual ~FtpDownload() = default;
virtual void loop() = 0;
virtual bool isActive() = 0;
};

class FtpUpload {
public:
virtual ~FtpUpload() = default;
virtual void loop() = 0;
virtual bool isActive() = 0;
};

class FtpClient {
public:
virtual ~FtpClient() = default;

virtual std::unique_ptr<FtpDownload> getFile(
const char *ftp_url, // ftp[s]://[user[:pass]@]host[:port][/directory]/filename
std::function<size_t(unsigned char *data, size_t len)> fileWriter,
std::function<void()> onClose,
std::function<void(MO_FtpCloseReason reason)> onClose,
const char *ca_cert = nullptr) = 0; // nullptr to disable cert check; will be ignored for non-TLS connections

virtual std::unique_ptr<FtpUpload> postFile(
const char *ftp_url, // ftp[s]://[user[:pass]@]host[:port][/directory]/filename
std::function<size_t(unsigned char *out, size_t buffsize)> fileReader, //write at most buffsize bytes into out-buffer. Return number of bytes written
std::function<void()> onClose,
std::function<void(MO_FtpCloseReason reason)> onClose,
const char *ca_cert = nullptr) = 0; // nullptr to disable cert check; will be ignored for non-TLS connections
};

Expand Down
Loading

0 comments on commit 5956d12

Please sign in to comment.