|
3 | 3 | // MIT License |
4 | 4 |
|
5 | 5 | #include <MicroOcpp/Core/Context.h> |
6 | | -#include <MicroOcpp/Core/Request.h> |
7 | 6 | #include <MicroOcpp/Core/Connection.h> |
8 | | -#include <MicroOcpp/Model/Model.h> |
| 7 | +#include <MicroOcpp/Core/FilesystemAdapter.h> |
| 8 | +#include <MicroOcpp/Core/Ftp.h> |
9 | 9 |
|
10 | 10 | #include <MicroOcpp/Debug.h> |
11 | 11 |
|
12 | 12 | using namespace MicroOcpp; |
13 | 13 |
|
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") { |
16 | 15 |
|
17 | 16 | } |
18 | 17 |
|
19 | 18 | 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 | + } |
20 | 30 |
|
| 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; |
21 | 57 | } |
22 | 58 |
|
| 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 | + |
23 | 129 | void Context::loop() { |
24 | 130 | connection.loop(); |
25 | 131 | reqQueue.loop(); |
26 | 132 | model.loop(); |
27 | 133 | } |
28 | 134 |
|
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 | | - |
37 | 135 | Model& Context::getModel() { |
38 | 136 | return model; |
39 | 137 | } |
|
0 commit comments