ThingsBoard Client SDK 0.16.0
Client SDK to connect with ThingsBoard IoT Platform from IoT devices (Arduino, Espressif, etc.)
Loading...
Searching...
No Matches
SDCard_Updater.h
Go to the documentation of this file.
1#ifndef SDCard_Updater_h
2#define SDCard_Updater_h
3
4// Local include.
5#include "Configuration.h"
6
7// Local include.
8#include <IUpdater.h>
9
10constexpr char OPEN_FILE_FAILED[] = "Failed to open file (%s), ensure path is correct and SD card exist and is initalized";
11
12
16template <typename Logger = DefaultLogger>
17class SDCard_Updater : public IUpdater {
18 public:
22 SDCard_Updater(char const * file_path)
23 : m_path(file_path)
24 {
25 // Nothing to do
26 }
27
31 SDCard_Updater(SDCard_Updater const & other) = delete;
32
36 void operator=(SDCard_Updater const & other) = delete;
37
38 ~SDCard_Updater() override {
39 reset();
40 }
41
42 bool begin(size_t const & firmware_size) override {
43 FILE* file = fopen(m_path, "w");
44 if (file == nullptr) {
45 Logger::printfln(OPEN_FILE_FAILED, m_path);
46 return false;
47 }
48 fclose(file);
49 return true;
50 }
51
52 size_t write(uint8_t * payload, size_t const & total_bytes) override {
53 FILE* file = fopen(m_path, "a");
54 if (file == nullptr) {
55 Logger::printfln(OPEN_FILE_FAILED, m_path);
56 return 0;
57 }
58 auto const bytes_written = fwrite(payload, 1, total_bytes, file);
59 fclose(file);
60 return bytes_written;
61 }
62
63 void reset() override {
64 end();
65 }
66
67 bool end() override {
68 return remove(m_path) == 0;
69 }
70
71 private:
72 char const * m_path = {}; // Path to the file the binary data is written into
73};
74
75#endif // SDCard_Updater_h
constexpr char OPEN_FILE_FAILED[]
Definition: SDCard_Updater.h:10
Updater interface that contains the method that a class that can be used to flash given binary data o...
Definition: IUpdater.h:14
IUpdater implementation that uses the c fopen function (https://cplusplus.com/reference/cstdio/fopen/...
Definition: SDCard_Updater.h:17
bool end() override
Ends the update and returns wheter it was successfully completed.
Definition: SDCard_Updater.h:67
size_t write(uint8_t *payload, size_t const &total_bytes) override
Writes the given amount of bytes of the packet data.
Definition: SDCard_Updater.h:52
void operator=(SDCard_Updater const &other)=delete
Deleted copy assignment operator.
SDCard_Updater(SDCard_Updater const &other)=delete
Deleted copy constructor.
void reset() override
Resets the writing of the given data so it can be restarted with begin.
Definition: SDCard_Updater.h:63
SDCard_Updater(char const *file_path)
Constructor.
Definition: SDCard_Updater.h:22
bool begin(size_t const &firmware_size) override
Initalizes the writing of the given data.
Definition: SDCard_Updater.h:42
~SDCard_Updater() override
Definition: SDCard_Updater.h:38