Skip to content

Commit c381f88

Browse files
committed
simplify Context and Model (WIP)
1 parent 0455ec0 commit c381f88

24 files changed

+898
-880
lines changed

CMakeLists.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# matth-x/MicroOcpp
2-
# Copyright Matthias Akstaller 2019 - 2024
2+
# Copyright Matthias Akstaller 2019 - 2025
33
# MIT License
44

55
cmake_minimum_required(VERSION 3.15)
@@ -77,12 +77,11 @@ set(MO_SRC
7777
src/MicroOcpp/Model/Certificates/Certificate_c.cpp
7878
src/MicroOcpp/Model/Certificates/CertificateMbedTLS.cpp
7979
src/MicroOcpp/Model/Certificates/CertificateService.cpp
80-
src/MicroOcpp/Model/ConnectorBase/ConnectorsCommon.cpp
80+
src/MicroOcpp/Model/ConnectorBase/ConnectorService.cpp
8181
src/MicroOcpp/Model/ConnectorBase/Connector.cpp
8282
src/MicroOcpp/Model/Diagnostics/DiagnosticsService.cpp
8383
src/MicroOcpp/Model/FirmwareManagement/FirmwareService.cpp
8484
src/MicroOcpp/Model/Heartbeat/HeartbeatService.cpp
85-
src/MicroOcpp/Model/Metering/MeteringConnector.cpp
8685
src/MicroOcpp/Model/Metering/MeteringService.cpp
8786
src/MicroOcpp/Model/Metering/MeterStore.cpp
8887
src/MicroOcpp/Model/Metering/MeterValue.cpp

src/MicroOcpp.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <MicroOcpp/Model/Model.h>
99
#include <MicroOcpp/Model/Metering/MeteringService.h>
1010
#include <MicroOcpp/Model/SmartCharging/SmartChargingService.h>
11-
#include <MicroOcpp/Model/ConnectorBase/ConnectorsCommon.h>
11+
#include <MicroOcpp/Model/ConnectorBase/ConnectorService.h>
1212
#include <MicroOcpp/Model/Heartbeat/HeartbeatService.h>
1313
#include <MicroOcpp/Model/FirmwareManagement/FirmwareService.h>
1414
#include <MicroOcpp/Model/Diagnostics/DiagnosticsService.h>
@@ -48,10 +48,6 @@ Connection *connection {nullptr};
4848
Context *context {nullptr};
4949
std::shared_ptr<FilesystemAdapter> filesystem;
5050

51-
#ifndef MO_NUMCONNECTORS
52-
#define MO_NUMCONNECTORS 2
53-
#endif
54-
5551
#define OCPP_ID_OF_CP 0
5652
#define OCPP_ID_OF_CONNECTOR 1
5753

@@ -300,8 +296,8 @@ void mocpp_initialize(Connection& connection, const char *bootNotificationCreden
300296
{
301297
model.setTransactionStore(std::unique_ptr<TransactionStore>(
302298
new TransactionStore(MO_NUMCONNECTORS, filesystem)));
303-
model.setConnectorsCommon(std::unique_ptr<ConnectorsCommon>(
304-
new ConnectorsCommon(*context, MO_NUMCONNECTORS, filesystem)));
299+
model.setConnectorService(std::unique_ptr<ConnectorService>(
300+
new ConnectorService(*context, MO_NUMCONNECTORS, filesystem)));
305301
auto connectors = makeVector<std::unique_ptr<Connector>>("v16.ConnectorBase.Connector");
306302
for (unsigned int connectorId = 0; connectorId < MO_NUMCONNECTORS; connectorId++) {
307303
connectors.emplace_back(new Connector(*context, filesystem, connectorId));

src/MicroOcpp/Core/Connection.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ class Connection {
4040
virtual bool sendTXT(const char *msg, size_t length) = 0;
4141

4242
/*
43+
* Pass incoming data from the server to this function. The OCPP library will consume it
4344
* The OCPP library calls this function once during initialization. It passes a callback function to
4445
* the socket. The socket should forward any incoming payload from the OCPP server to the receiveTXT callback
4546
*/
46-
virtual void setReceiveTXTcallback(ReceiveTXTcallback &receiveTXT) = 0;
47+
bool recvTXT(const char *msg, size_t len) = 0;
4748

4849
/*
4950
* Returns the timestamp of the last incoming message. Use mocpp_tick_ms() for creating the correct timestamp

src/MicroOcpp/Core/Context.cpp

Lines changed: 110 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,135 @@
33
// MIT License
44

55
#include <MicroOcpp/Core/Context.h>
6-
#include <MicroOcpp/Core/Request.h>
76
#include <MicroOcpp/Core/Connection.h>
8-
#include <MicroOcpp/Model/Model.h>
7+
#include <MicroOcpp/Core/FilesystemAdapter.h>
8+
#include <MicroOcpp/Core/Ftp.h>
99

1010
#include <MicroOcpp/Debug.h>
1111

1212
using namespace MicroOcpp;
1313

14-
Context::Context(Connection& connection, std::shared_ptr<FilesystemAdapter> filesystem, uint16_t bootNr, ProtocolVersion version)
15-
: MemoryManaged("Context"), connection(connection), model{version, bootNr}, reqQueue{connection, operationRegistry} {
14+
Context::Context() : MemoryManaged("Context") {
1615

1716
}
1817

1918
Context::~Context() {
19+
if (filesystem && isFilesystemOwner) {
20+
delete filesystem;
21+
filesystem = nullptr;
22+
isFilesystemOwner = false;
23+
}
24+
25+
if (connection && isConnectionOwner) {
26+
delete connection;
27+
connection = nullptr;
28+
isConnectionOwner = false;
29+
}
2030

31+
if (ftpClient && isFtpClientOwner) {
32+
delete ftpClient;
33+
ftpClient = nullptr;
34+
isFtpClientOwner = false;
35+
}
36+
}
37+
38+
void Context::setDebugCb(void (*debugCb)(const char *msg)) {
39+
this->debugCb = debugCb;
40+
}
41+
42+
void Context::setDebugCb2(void (*debugCb2)(int lvl, const char *fn, int line, const char *msg)) {
43+
this->debugCb2 = debugCb2;
44+
}
45+
46+
void Context::setTicksCb(unsigned long (*ticksCb)()) {
47+
this->ticksCb = ticksCb;
48+
}
49+
50+
void Context::setFilesystemAdapter(FilesystemAdapter *filesystem) {
51+
if (this->filesystem && isFilesystemOwner) {
52+
delete this->filesystem;
53+
this->filesystem = nullptr;
54+
isFilesystemOwner = false;
55+
}
56+
this->filesystem = filesystem;
2157
}
2258

59+
FilesystemAdapter *Context::getFilesystemAdapter() {
60+
return filesystem;
61+
}
62+
63+
void Context::setConnection(Connection *connection) {
64+
if (connection && isConnectionOwner) {
65+
delete connection;
66+
connection = nullptr;
67+
isConnectionOwner = false;
68+
}
69+
this->connection = connection;
70+
}
71+
72+
Connection *Context::getConnection() {
73+
return connection;
74+
}
75+
76+
void Context::setFtpClient(FtpClient *ftpClient) {
77+
if (ftpClient && isFtpClientOwner) {
78+
delete ftpClient;
79+
ftpClient = nullptr;
80+
isFtpClientOwner = false;
81+
}
82+
ftpClient = ftpClient;
83+
}
84+
85+
FtpClient *Context::getFtpClient() {
86+
return ftpClient;
87+
}
88+
89+
bool Context::setup(int protocolVersion) {
90+
91+
}
92+
93+
void Context::loop();
94+
95+
Model& getModel();
96+
97+
OperationRegistry& getOperationRegistry();
98+
99+
RequestQueue& getRequestQueue();
100+
101+
const ProtocolVersion& getVersion();
102+
};
103+
104+
} //end namespace MicroOcpp
105+
106+
#endif // __cplusplus
107+
108+
#ifdef __cplusplus
109+
extern "C" {
110+
#endif // __cplusplus
111+
112+
struct mo_context;
113+
typedef struct mo_context mo_context;
114+
115+
mo_context *mo_context_make();
116+
117+
void mo_context_free(mo_context *ctx);
118+
119+
void mo_context_dbg_cb_set(mo_context *ctx, void (*debug_cb)(const char *msg));
120+
121+
void mo_context_dbg_cb2_set(mo_context *ctx, void (*debug_cb)(int lvl, const char *fn, int line, const char *msg));
122+
123+
void mo_context_ticks_cb_set(unsigned long (*ticksCb)());
124+
125+
bool mo_context_setup(mo_context *ctx, int protocol_version);
126+
127+
void mo_context_loop(mo_context *ctx);
128+
23129
void Context::loop() {
24130
connection.loop();
25131
reqQueue.loop();
26132
model.loop();
27133
}
28134

29-
void Context::initiateRequest(std::unique_ptr<Request> op) {
30-
if (!op) {
31-
MO_DBG_ERR("invalid arg");
32-
return;
33-
}
34-
reqQueue.sendRequest(std::move(op));
35-
}
36-
37135
Model& Context::getModel() {
38136
return model;
39137
}

src/MicroOcpp/Core/Context.h

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,99 @@
11
// matth-x/MicroOcpp
2-
// Copyright Matthias Akstaller 2019 - 2024
2+
// Copyright Matthias Akstaller 2019 - 2025
33
// MIT License
44

55
#ifndef MO_CONTEXT_H
66
#define MO_CONTEXT_H
77

8-
#include <memory>
9-
8+
#include <MicroOcpp/Model/Model.h>
109
#include <MicroOcpp/Core/OperationRegistry.h>
1110
#include <MicroOcpp/Core/RequestQueue.h>
1211
#include <MicroOcpp/Core/Memory.h>
13-
#include <MicroOcpp/Core/Ftp.h>
14-
#include <MicroOcpp/Model/Model.h>
1512
#include <MicroOcpp/Version.h>
1613

14+
#ifdef __cplusplus
15+
1716
namespace MicroOcpp {
1817

19-
class Connection;
2018
class FilesystemAdapter;
19+
class Connection;
20+
class FtpClient;
2121

2222
class Context : public MemoryManaged {
2323
private:
24-
Connection& connection;
25-
OperationRegistry operationRegistry;
24+
void (*debugCb)(const char *msg) = nullptr;
25+
void (*debugCb2)(int lvl, const char *fn, int line, const char *msg) = nullptr;
26+
27+
unsigned long (*ticksCb)() = nullptr;
28+
29+
FilesystemAdapter *filesystem = nullptr;
30+
Connection *connection = nullptr;
31+
FtpClient *ftpClient = nullptr;
32+
2633
Model model;
34+
OperationRegistry operationRegistry;
2735
RequestQueue reqQueue;
2836

29-
std::unique_ptr<FtpClient> ftpClient;
37+
bool isFilesystemOwner = false;
38+
bool isConnectionOwner = false;
39+
bool isFtpClientOwner = false;
3040

3141
public:
32-
Context(Connection& connection, std::shared_ptr<FilesystemAdapter> filesystem, uint16_t bootNr, ProtocolVersion version);
42+
Context();
3343
~Context();
3444

35-
void loop();
45+
void setDebugCb(void (*debugCb)(const char *msg));
46+
void setDebugCb2(void (*debugCb2)(int lvl, const char *fn, int line, const char *msg));
47+
48+
void setTicksCb(unsigned long (*ticksCb)());
3649

37-
void initiateRequest(std::unique_ptr<Request> op);
50+
void setFilesystemAdapter(FilesystemAdapter *filesystem);
51+
FilesystemAdapter *getFilesystemAdapter();
52+
53+
void setConnection(Connection *connection);
54+
Connection *getConnection();
55+
56+
void setFtpClient(FtpClient *ftpClient);
57+
FtpClient *getFtpClient();
3858

3959
Model& getModel();
4060

41-
OperationRegistry& getOperationRegistry();
61+
bool setup(int ocppVersion);
4262

43-
const ProtocolVersion& getVersion();
63+
void loop();
4464

45-
Connection& getConnection();
65+
OperationRegistry& getOperationRegistry();
4666

4767
RequestQueue& getRequestQueue();
48-
49-
void setFtpClient(std::unique_ptr<FtpClient> ftpClient);
50-
FtpClient *getFtpClient();
5168
};
5269

5370
} //end namespace MicroOcpp
5471

72+
#endif // __cplusplus
73+
74+
#ifdef __cplusplus
75+
extern "C" {
76+
#endif // __cplusplus
77+
78+
struct mo_context;
79+
typedef struct mo_context mo_context;
80+
81+
mo_context *mo_context_make();
82+
83+
void mo_context_free(mo_context *ctx);
84+
85+
void mo_context_dbg_cb_set(mo_context *ctx, void (*debug_cb)(const char *msg));
86+
87+
void mo_context_dbg_cb2_set(mo_context *ctx, void (*debug_cb)(int lvl, const char *fn, int line, const char *msg));
88+
89+
void mo_context_ticks_cb_set(unsigned long (*ticksCb)());
90+
91+
bool mo_context_setup(mo_context *ctx, int ocpp_version);
92+
93+
void mo_context_loop(mo_context *ctx);
94+
95+
#ifdef __cplusplus
96+
} // extern "C"
97+
#endif // __cplusplus
98+
5599
#endif

src/MicroOcpp/Core/RequestQueue.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ class RequestQueue : public MemoryManaged {
8383
void sendRequest(std::unique_ptr<Request> request); //send an OCPP operation request to the server; adds request to default queue
8484
void sendRequestPreBoot(std::unique_ptr<Request> request); //send an OCPP operation request to the server; adds request to preBootQueue
8585

86+
void handleRequest(const char *operationType, Operation* (*createOperationCb)(const char *operationType, void* user_data), void *user_data = nullptr);
87+
void setOnRequest(const char *operationType, void (*onRequest)(const char *payloadJson, size_t len));
88+
void setOnConfirmation(const char *operationType, void (*onConfirmation)(const char *payloadJson, size_t len));
89+
8690
void addSendQueue(RequestEmitter* sendQueue);
8791
void setPreBootSendQueue(VolatileRequestQueue *preBootQueue);
8892

src/MicroOcpp/Core/Time.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,10 @@ namespace MicroOcpp {
2626

2727
class Timestamp : public MemoryManaged {
2828
private:
29-
/*
30-
* Internal representation of the current time. The initial values correspond to UNIX-time 0. January
31-
* corresponds to month 0 and the first day in the month is day 0.
32-
*/
33-
int16_t year = 1970;
34-
int16_t month = 0;
35-
int16_t day = 0;
36-
int32_t hour = 0;
37-
int32_t minute = 0;
38-
int32_t second = 0;
29+
int32_t time; //Unix time (number of seconds since Jan 1, 1970 UTC, not counting leap seconds)
30+
3931
#if MO_ENABLE_TIMESTAMP_MILLISECONDS
40-
int32_t ms = 0;
32+
int16_t ms = 0; //fractional ms of timestamp. Compound timestamp = time + ms. Range should be [0, 999]
4133
#endif //MO_ENABLE_TIMESTAMP_MILLISECONDS
4234

4335
public:
@@ -83,6 +75,8 @@ class Timestamp : public MemoryManaged {
8375

8476
int operator-(const Timestamp &rhs) const;
8577

78+
operator int32_t() const;
79+
8680
friend Timestamp operator+(const Timestamp &lhs, int secs);
8781
friend Timestamp operator-(const Timestamp &lhs, int secs);
8882

0 commit comments

Comments
 (0)