8000 Sat Jun 10 13:12:27 CDT 2017 · esp32vn/esp32-snippets@f32e5bd · GitHub
[go: up one dir, main page]

Skip to content

Commit f32e5bd

Browse files
author
kolban
committed
Sat Jun 10 13:12:27 CDT 2017
1 parent 0294717 commit f32e5bd

17 files changed

+1333
-138
lines changed

cpp_utils/Doxyfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,8 @@ EXCLUDE_PATTERNS =
890890
# Note that the wildcards are matched against the file with absolute path, so to
891891
# exclude all test directories use the pattern */test/*
892892

893-
EXCLUDE_SYMBOLS =
893+
EXCLUDE_SYMBOLS = profile_inst \
894+
data_packet
894895

895896
# The EXAMPLE_PATH tag can be used to specify one or more files or directories
896897
# that contain example code fragments that are included (see the \include

cpp_utils/FATFS_VFS.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ extern "C" {
1313

1414
/**
1515
* @brief Constructor.
16+
*
17+
* In ESP32, every file system has a mount point. If a file access is attempted south of that
18+
* mount point, then the corresponding file system will be used. The `mountPath` parameter defines
19+
* the mount point for this instance of the FATFS file system.
20+
* In order to save the files for subsequent retrieval, the file data has to be written to flash memory.
21+
* A partition table provides a map or layout of the flash memory by defining named partitions. The
22+
* `partitionName` parameter defines the name of the partition used to provide the underlying storage
23+
* for this instance of the FATFS file system.
24+
*
1625
* @param [in] mountPath The path in the VFS where the FAT file system should be mounted.
1726
* @param [in] partitionName The name of the partition used to store the FAT file system.
1827
*/

cpp_utils/FATFS_VFS.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ extern "C" {
1313
}
1414
/**
1515
* @brief Provide access to the FAT file system on %SPI flash.
16+
* The FATFS_VFS file system needs a partition definition. This is a map of flash memory that
17+
* specified an array into which the files should be saved and loaded. A partition is a named
18+
* entity and the name we choose in the partition definition should be named in the constructor.
19+
*
20+
* A partition configuration file can be described in the `make menuconfig` settings. For example:
21+
* ~~~~
22+
* nvs, data, nvs, 0x9000, 0x6000,
23+
* phy_init, data, phy, 0xf000, 0x1000,
24+
* factory, app, factory, 0x10000, 1M,
25+
* storage, data, fat, , 1M,
26+
* ~~~~
27+
*
28+
* The recommended file name for the partition description is `partitions.csv`
1629
*
1730
* A typical example would be:
1831
*

cpp_utils/File.cpp

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* File.cpp
3+
*
4+
* Created on: Jun 1, 2017
5+
* Author: kolban
6+
*/
7+
8+
#include "File.h"
9+
#include <sys/stat.h>
10+
#include <esp_log.h>
11+
#include <string>
12+
#include <stdlib.h>
13+
#include <stdio.h>
14+
#include <GeneralUtils.h>
15+
16+
static const char tag[] = "File";
17+
/**
18+
* @brief Construct a file.
19+
* @param [in] name The name of the file.
20+
* @param [in] type The type of the file (DT_REGULAR, DT_DIRECTORY or DT_UNKNOWN).
21+
*/
22+
File::File(std::string name, uint8_t type) {
23+
m_name = name;
24+
m_type = type;
25+
}
26+
27+
File::~File() {
28+
}
29+
30+
31+
/**
32+
* @brief Retrieve the content of the file.
33+
* @param [in] base64Encode Should we base64 encode the content?
34+
* @return The content of the file.
35+
*/
36+
std::string File::getContent(bool base64Encode) {
37+
uint32_t size = length();
38+
ESP_LOGD(tag, "File:: getContent(), name=%s, length=%d", m_name.c_str(), size);
39+
if (size == 0) {
40+
return "";
41+
}
42+
uint8_t *pData = (uint8_t *)malloc(size);
43+
if (pData == nullptr) {
44+
ESP_LOGE(tag, "getContent: Failed to allocate memory");
45+
return "";
46+
}
47+
FILE *file = fopen(m_name.c_str(), "r");
48+
fread(pData, size, 1, file);
49+
fclose(file);
50+
std::string ret((char *)pData, size);
51+
free(pData);
52+
if (base64Encode) {
53+
std::string encoded;
54+
GeneralUtils::base64Decode(ret, &encoded);
55+
return encoded;
56+
}
57+
return ret;
58+
} // getContent
59+
60+
61+
/**
62+
* @brief Retrieve the content of the file.
63+
* @param [in] offset The file offset to read from.
64+
* @param [in] readSize The number of bytes to read.
65+
* @return The content of the file.
66+
*/
67+
std::string File::getContent(uint32_t offset, uint32_t readSize) {
68+
uint32_t fileSize = length();
69+
ESP_LOGD(tag, "File:: getContent(), name=%s, fileSize=%d, offset=%d, readSize=%d",
70+
m_name.c_str(), fileSize, offset, readSize);
71+
if (fileSize == 0 || offset > fileSize) {
72+
return "";
73+
}
74+
uint8_t *pData = (uint8_t *)malloc(readSize);
75+
if (pData == nullptr) {
76+
ESP_LOGE(tag, "getContent: Failed to allocate memory");
77+
return "";
78+
}
79+
FILE *file = fopen(m_name.c_str(), "r");
80+
fseek(file, offset, SEEK_SET);
81+
size_t bytesRead = fread(pData, 1, readSize, file);
82+
fclose(file);
83+
std::string ret((char *)pData, bytesRead);
84+
free(pData);
85+
return ret;
86+
}
87+
88+
89+
/**
90+
* @brief Get the name of the file.
91+
* @return The name of the file.
92+
*/
93+
std::string File::getName() {
94+
return m_name;
95+
} // getName
96+
97+
98+
/**
99+
* @brief Get the type of the file.
100+
* The type of a file can be DT_REGULAR, DT_DIRECTORY or DT_UNKNOWN.
101+
* @return The type of the file.
102+
*/
103+
uint8_t File::getType() {
104+
return m_type;
105+
} // getName
106+
107+
108+
/**
109+
* @brief Get the length of the file in bytes.
110+
* @return The length of the file in bytes.
111+
*/
112+
uint32_t File::length() {
113+
struct stat buf;
114+
int rc = stat(m_name.c_str(), &buf);
115+
if (rc != 0) {
116+
return 0;
117+
}
118+
return buf.st_size;
119+
} // length
120+
121+
122+
/**
123+
* @brief Determine if the type of the file is a directory.
124+
* @return True if the file is a directory.
125+
*/
126+
bool File::isDirectory() {
127+
struct stat buf;
128+
int rc = stat(m_name.c_str(), &buf);
129+
if (rc != 0) {
130+
return false;
131+
}
132+
if (S_ISDIR(buf.st_mode)) {
133+
return true;
134+
}
135+
return false;
136+
} // isDirectory
137+
138+

cpp_utils/File.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* File.h
3+
*
4+
* Created on: Jun 1, 2017
5+
* Author: kolban
6+
*/
7+
8+
#ifndef COMPONENTS_CPP_UTILS_FILE_H_
9+
#define COMPONENTS_CPP_UTILS_FILE_H_
10+
#include <string>
11+
#include <dirent.h>
12+
13+
/**
14+
* @brief A logical representation of a file.
15+
*/
16+
class File {
17+
public:
18+
File(std::string name, uint8_t type = DT_UNKNOWN);
19+
virtual ~File();
20+
21+
std::string getContent(bool base64Encode=false);
22+
std::string getContent(uint32_t offset, uint32_t size);
23+
std::string getName();
24+
uint8_t getType();
25+
bool isDirectory();
26+
uint32_t length();
27+
28+
private:
29+
std::string m_name;
30+
uint8_t m_type;
31+
};
32+
33+
#endif /* COMPONENTS_CPP_UTILS_FILE_H_ */

cpp_utils/FileSystem.cpp

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,25 @@
44
* Created on: May 20, 2017
55
* Author: kolban
66
*/
7+
#include <iostream>
8+
#include <sstream>
79
#include <string>
8-
#include <esp_log.h>
10+
911
#include <dirent.h>
1012
#include <errno.h>
1113
#include <sys/stat.h>
14+
#include <unistd.h>
15+
16+
#include <esp_log.h>
17+
1218
#include "FileSystem.h"
1319

1420
static char tag[] = "FileSystem";
1521

1622
FileSystem::FileSystem() {
17-
// TODO Auto-generated constructor stub
18-
1923
}
2024

2125
FileSystem::~FileSystem() {
22-
// TODO Auto-generated destructor stub
2326
}
2427

2528

@@ -54,10 +57,89 @@ void FileSystem::dumpDirectory(std::string path) {
5457
::closedir(pDir);
5558
} // dumpDirectory
5659

60+
61+
/**
62+
* @brief Get the contents of a directory.
63+
10000 * @param [in] path The path to the directory.
64+
* @return A vector of Files in the directory.
65+
*/
66+
std::vector<File> FileSystem::getDirectoryContents(std::string path) {
67+
std::vector<File> ret;
68+
DIR *pDir = ::opendir(path.c_str());
69+
if (pDir == nullptr) {
70+
ESP_LOGE(tag, "getDirectoryContents:: Unable to open directory: %s [errno=%d]", path.c_str(), errno);
71+
return ret;
72+
}
73+
struct dirent *pDirent;
74+
ESP_LOGD(tag, "Directory dump of %s", path.c_str());
75+
while((pDirent = readdir(pDir)) != nullptr) {
76+
File file(std::string(pDirent->d_name), pDirent->d_type);
77+
ret.push_back(file);
78+
}
79+
::closedir(pDir);
80+
return ret;
81+
} // getDirectoryContents
82+
83+
84+
/**
85+
* @brief Create a directory
86+
* @param [in] path The directory to create.
87+
* @return N/A.
88+
*/
5789
int FileSystem::mkdir(std::string path) {
5890
int rc = ::mkdir(path.c_str(), 0);
5991
if (rc != 0) {
6092
ESP_LOGE(tag, "mkdir: errno=%d", errno);
93+
rc = errno;
6194
}
6295
return rc;
63-
}
96+
} // mkdir
97+
98+
99+
/**
100+
* @brief Return the constituent parts of the path.
101+
* If we imagine a path as composed of parts separated by slashes, then this function
102+
* returns a vector composed of the parts. For example:
103+
*
104+
* ```
105+
* /x/y/z
106+
* ```
107+
* will break out to:
108+
*
109+
* ```
110+
* path[0] = ""
111+
* path[1] = "x"
112+
* path[2] = "y"
113+
* path[3] = "z"
114+
* ```
115+
*
116+
* @return A vector of the constituent parts of the path.
117+
*/
118+
std::vector<std::string> FileSystem::pathSplit(std::string path) {
119+
std::istringstream stream(path);
120+
std::vector<std::string> ret;
121+
std::string pathPart;
122+
while(std::getline(stream, pathPart, '/')) {
123+
ret.push_back(pathPart);
124+
}
125+
// Debug
126+
for (int i=0; i<ret.size(); i++) {
127+
ESP_LOGD(tag, "part[%d]: %s", i, ret[i].c_str());
128+
}
129+
return ret;
130+
} // pathSplit
131+
132+
133+
/**
134+
* @brief Remove a file from the file system.
135+
* @param [in] path The path to the file to be removed.
136+
* @return The return code of the underlying call.
137+
*/
138+
int FileSystem::remove(std::string path) {
139+
int rc = ::unlink(path.c_str());
140+
if (rc != 0) {
141+
ESP_LOGE(tag, "unlink: errno=%d", errno);
142+
rc = errno;
143+
}
144+
return rc;
145+
} // remove

cpp_utils/FileSystem.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@
88
#ifndef COMPONENTS_CPP_UTILS_FILESYSTEM_H_
99
#define COMPONENTS_CPP_UTILS_FILESYSTEM_H_
1010
#include <string>
11+
#include <vector>
12+
#include <File.h>
1113
/**
1214
* @brief File system utilities.
1315
*/
1416
class FileSystem {
1517
public:
1618
FileSystem();
1719
virtual ~FileSystem();
20+
static std::vector<File> getDirectoryContents(std:: string path);
1821
static void dumpDirectory(std::string path);
1922
static int mkdir(std::string path);
23+
static std::vector<std::string> pathSplit(std::string path);
24+
static int remove(std::string path);
2025
};
2126

2227
#endif /* COMPONENTS_CPP_UTILS_FILESYSTEM_H_ */

0 commit comments

Comments
 (0)
0