Skip to content

Commit 0458c11

Browse files
tested examples, added documentation
1 parent 9dfc05c commit 0458c11

File tree

18 files changed

+1905
-796
lines changed

18 files changed

+1905
-796
lines changed
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
11
# SPDX-License-Identifier: Apache-2.0
2+
# Header-only library - just expose include directory
23
zephyr_include_directories(.)
3-
4-
if(NOT DEFINED ARDUINO_BUILD_PATH)
5-
zephyr_sources_ifdef(CONFIG_FILE_SYSTEM
6-
StorageError.cpp
7-
StorageFile.cpp
8-
StorageFolder.cpp
9-
)
10-
endif()

libraries/ArduinoStorage/StorageError.cpp

Lines changed: 0 additions & 96 deletions
This file was deleted.

libraries/ArduinoStorage/StorageError.h

Lines changed: 85 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define ARDUINO_STORAGE_ERROR_H
99

1010
#include <Arduino.h>
11+
#include <cstring>
1112

1213
enum class StorageErrorCode {
1314
NONE = 0,
@@ -49,19 +50,96 @@ enum class StorageErrorCode {
4950

5051
class StorageError {
5152
public:
52-
StorageError();
53+
StorageError() : code_(StorageErrorCode::NONE) {
54+
message_[0] = '\0';
55+
}
5356

5457
// Error state
55-
StorageErrorCode getCode() const;
56-
const char* getMessage() const;
57-
bool hasError() const;
58+
StorageErrorCode getCode() const {
59+
return code_;
60+
}
61+
62+
const char* getMessage() const {
63+
if (message_[0] != '\0') {
64+
return message_;
65+
}
66+
67+
// Return default message based on error code
68+
switch (code_) {
69+
case StorageErrorCode::NONE:
70+
return "No error";
71+
case StorageErrorCode::FILE_NOT_FOUND:
72+
return "File not found";
73+
case StorageErrorCode::FOLDER_NOT_FOUND:
74+
return "Folder not found";
75+
case StorageErrorCode::ALREADY_EXISTS:
76+
return "Already exists";
77+
case StorageErrorCode::INVALID_PATH:
78+
return "Invalid path";
79+
case StorageErrorCode::PERMISSION_DENIED:
80+
return "Permission denied";
81+
case StorageErrorCode::READ_ERROR:
82+
return "Read error";
83+
case StorageErrorCode::WRITE_ERROR:
84+
return "Write error";
85+
case StorageErrorCode::SEEK_ERROR:
86+
return "Seek error";
87+
case StorageErrorCode::OPEN_ERROR:
88+
return "Open error";
89+
case StorageErrorCode::CLOSE_ERROR:
90+
return "Close error";
91+
case StorageErrorCode::STORAGE_FULL:
92+
return "Storage full";
93+
case StorageErrorCode::STORAGE_NOT_MOUNTED:
94+
return "Storage not mounted";
95+
case StorageErrorCode::STORAGE_CORRUPTED:
96+
return "Storage corrupted";
97+
case StorageErrorCode::STORAGE_NOT_FORMATTED:
98+
return "Storage not formatted";
99+
case StorageErrorCode::INVALID_OPERATION:
100+
return "Invalid operation";
101+
case StorageErrorCode::INVALID_MODE:
102+
return "Invalid mode";
103+
case StorageErrorCode::BUFFER_OVERFLOW:
104+
return "Buffer overflow";
105+
case StorageErrorCode::OUT_OF_MEMORY:
106+
return "Out of memory";
107+
case StorageErrorCode::TIMEOUT:
108+
return "Timeout";
109+
case StorageErrorCode::HARDWARE_ERROR:
110+
return "Hardware error";
111+
case StorageErrorCode::NOT_INITIALIZED:
112+
return "Not initialized";
113+
case StorageErrorCode::UNKNOWN_ERROR:
114+
default:
115+
return "Unknown error";
116+
}
117+
}
118+
119+
bool hasError() const {
120+
return code_ != StorageErrorCode::NONE;
121+
}
58122

59123
// Error setting (for implementations)
60-
void setError(StorageErrorCode code, const char* message = nullptr);
61-
void clear();
124+
void setError(StorageErrorCode code, const char* message = nullptr) {
125+
code_ = code;
126+
if (message != nullptr) {
127+
strncpy(message_, message, sizeof(message_) - 1);
128+
message_[sizeof(message_) - 1] = '\0';
129+
} else {
130+
message_[0] = '\0';
131+
}
132+
}
133+
134+
void clear() {
135+
code_ = StorageErrorCode::NONE;
136+
message_[0] = '\0';
137+
}
62138

63139
// Convenience operators
64-
operator bool() const; // Returns true if error exists
140+
operator bool() const {
141+
return hasError();
142+
}
65143

66144
private:
67145
StorageErrorCode code_;

libraries/ArduinoStorage/StorageFile.cpp

Lines changed: 0 additions & 51 deletions
This file was deleted.

libraries/ArduinoStorage/StorageFile.h

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define ARDUINO_STORAGE_FILE_H
99

1010
#include <Arduino.h>
11+
#include <cstring>
1112
#include "StorageCommon.h"
1213
#include "StorageError.h"
1314

@@ -23,10 +24,25 @@ class Folder;
2324
*/
2425
class File {
2526
public:
26-
File();
27-
File(const char* path);
28-
File(const String& path);
29-
virtual ~File();
27+
File() {
28+
path_[0] = '\0';
29+
}
30+
31+
File(const char* path) {
32+
if (path != nullptr) {
33+
strncpy(path_, path, sizeof(path_) - 1);
34+
path_[sizeof(path_) - 1] = '\0';
35+
} else {
36+
path_[0] = '\0';
37+
}
38+
}
39+
40+
File(const String& path) {
41+
strncpy(path_, path.c_str(), sizeof(path_) - 1);
42+
path_[sizeof(path_) - 1] = '\0';
43+
}
44+
45+
virtual ~File() {}
3046

3147
// Opening and Closing
3248
virtual bool open(const char* filename, FileMode mode, StorageError* error = nullptr) = 0;
@@ -58,15 +74,31 @@ class File {
5874
virtual bool rename(const String& newFilename, StorageError* error = nullptr) = 0;
5975

6076
// Path Information
61-
virtual const char* getPath() const;
62-
virtual String getPathAsString() const;
63-
virtual String getFilename() const;
77+
virtual const char* getPath() const {
78+
return path_;
79+
}
80+
81+
virtual String getPathAsString() const {
82+
return String(path_);
83+
}
84+
85+
virtual String getFilename() const {
86+
const char* lastSep = strrchr(path_, '/');
87+
if (lastSep != nullptr) {
88+
return String(lastSep + 1);
89+
}
90+
return String(path_);
91+
}
6492

6593
protected:
6694
char path_[STORAGE_MAX_PATH_LENGTH];
6795

6896
// Helper to set error if pointer is not null
69-
static void setErrorIfNotNull(StorageError* error, StorageErrorCode code, const char* message = nullptr);
97+
static void setErrorIfNotNull(StorageError* error, StorageErrorCode code, const char* message = nullptr) {
98+
if (error != nullptr) {
99+
error->setError(code, message);
100+
}
101+
}
70102
};
71103

72104
#endif // ARDUINO_STORAGE_FILE_H

0 commit comments

Comments
 (0)