parent
10eb120878
commit
961c8014ad
@ -0,0 +1,60 @@ |
|||||||
|
#include "file_gatherer.hpp" |
||||||
|
|
||||||
|
#include <deque> |
||||||
|
#include <functional> |
||||||
|
#include <sstream> |
||||||
|
#include <string> |
||||||
|
|
||||||
|
#include "ff.h" |
||||||
|
|
||||||
|
namespace database { |
||||||
|
|
||||||
|
static_assert(sizeof(TCHAR) == sizeof(char), "TCHAR must be CHAR"); |
||||||
|
|
||||||
|
auto FileGathererImpl::FindFiles(const std::string& root, |
||||||
|
std::function<void(const std::string&)> cb) |
||||||
|
-> void { |
||||||
|
std::deque<std::string> to_explore; |
||||||
|
to_explore.push_back(root); |
||||||
|
|
||||||
|
while (!to_explore.empty()) { |
||||||
|
std::string next_path_str = to_explore.front(); |
||||||
|
const TCHAR* next_path = static_cast<const TCHAR*>(next_path_str.c_str()); |
||||||
|
|
||||||
|
FF_DIR dir; |
||||||
|
FRESULT res = f_opendir(&dir, next_path); |
||||||
|
if (res != FR_OK) { |
||||||
|
// TODO: log.
|
||||||
|
continue; |
||||||
|
} |
||||||
|
|
||||||
|
for (;;) { |
||||||
|
FILINFO info; |
||||||
|
res = f_readdir(&dir, &info); |
||||||
|
if (res != FR_OK || info.fname[0] == 0) { |
||||||
|
// No more files in the directory.
|
||||||
|
break; |
||||||
|
} else if (info.fattrib & (AM_HID | AM_SYS) || info.fname[0] == '.') { |
||||||
|
// System or hidden file. Ignore it and move on.
|
||||||
|
continue; |
||||||
|
} else { |
||||||
|
std::stringstream full_path; |
||||||
|
full_path << next_path_str << "/" << info.fname; |
||||||
|
|
||||||
|
if (info.fattrib & AM_DIR) { |
||||||
|
// This is a directory. Add it to the explore queue.
|
||||||
|
to_explore.push_back(full_path.str()); |
||||||
|
} else { |
||||||
|
// This is a file! Let the callback know about it.
|
||||||
|
// std::invoke(cb, full_path.str(), info);
|
||||||
|
std::invoke(cb, full_path.str()); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
f_closedir(&dir); |
||||||
|
to_explore.pop_front(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} // namespace database
|
@ -0,0 +1,22 @@ |
|||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <string> |
||||||
|
|
||||||
|
#include "song.hpp" |
||||||
|
|
||||||
|
namespace database { |
||||||
|
|
||||||
|
class ITagParser { |
||||||
|
public: |
||||||
|
virtual ~ITagParser() {} |
||||||
|
virtual auto ReadAndParseTags(const std::string& path, SongTags* out) |
||||||
|
-> bool = 0; |
||||||
|
}; |
||||||
|
|
||||||
|
class TagParserImpl : public ITagParser { |
||||||
|
public: |
||||||
|
virtual auto ReadAndParseTags(const std::string& path, SongTags* out) |
||||||
|
-> bool override; |
||||||
|
}; |
||||||
|
|
||||||
|
} // namespace database
|
@ -0,0 +1,107 @@ |
|||||||
|
#include "tag_parser.hpp" |
||||||
|
|
||||||
|
#include <esp_log.h> |
||||||
|
#include <ff.h> |
||||||
|
#include <tags.h> |
||||||
|
|
||||||
|
namespace database { |
||||||
|
|
||||||
|
namespace libtags { |
||||||
|
|
||||||
|
struct Aux { |
||||||
|
FIL file; |
||||||
|
FILINFO info; |
||||||
|
SongTags* tags; |
||||||
|
}; |
||||||
|
|
||||||
|
static int read(Tagctx* ctx, void* buf, int cnt) { |
||||||
|
Aux* aux = reinterpret_cast<Aux*>(ctx->aux); |
||||||
|
UINT bytes_read; |
||||||
|
if (f_read(&aux->file, buf, cnt, &bytes_read) != FR_OK) { |
||||||
|
return -1; |
||||||
|
} |
||||||
|
return bytes_read; |
||||||
|
} |
||||||
|
|
||||||
|
static int seek(Tagctx* ctx, int offset, int whence) { |
||||||
|
Aux* aux = reinterpret_cast<Aux*>(ctx->aux); |
||||||
|
FRESULT res; |
||||||
|
if (whence == 0) { |
||||||
|
// Seek from the start of the file. This is f_lseek's behaviour.
|
||||||
|
res = f_lseek(&aux->file, offset); |
||||||
|
} else if (whence == 1) { |
||||||
|
// Seek from current offset.
|
||||||
|
res = f_lseek(&aux->file, aux->file.fptr + offset); |
||||||
|
} else if (whence == 2) { |
||||||
|
// Seek from the end of the file
|
||||||
|
res = f_lseek(&aux->file, aux->info.fsize + offset); |
||||||
|
} else { |
||||||
|
return -1; |
||||||
|
} |
||||||
|
return res; |
||||||
|
} |
||||||
|
|
||||||
|
static void tag(Tagctx* ctx, |
||||||
|
int t, |
||||||
|
const char* k, |
||||||
|
const char* v, |
||||||
|
int offset, |
||||||
|
int size, |
||||||
|
Tagread f) { |
||||||
|
Aux* aux = reinterpret_cast<Aux*>(ctx->aux); |
||||||
|
if (t == Ttitle) { |
||||||
|
aux->tags->title = v; |
||||||
|
} else if (t == Tartist) { |
||||||
|
aux->tags->artist = v; |
||||||
|
} else if (t == Talbum) { |
||||||
|
aux->tags->album = v; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
static void toc(Tagctx* ctx, int ms, int offset) {} |
||||||
|
|
||||||
|
} // namespace libtags
|
||||||
|
|
||||||
|
static const std::size_t kBufSize = 1024; |
||||||
|
static const char* kTag = "TAGS"; |
||||||
|
|
||||||
|
auto TagParserImpl::ReadAndParseTags(const std::string& path, SongTags* out) |
||||||
|
-> bool { |
||||||
|
libtags::Aux aux; |
||||||
|
aux.tags = out; |
||||||
|
if (f_stat(path.c_str(), &aux.info) != FR_OK || |
||||||
|
f_open(&aux.file, path.c_str(), FA_READ) != FR_OK) { |
||||||
|
ESP_LOGW(kTag, "failed to open file %s", path.c_str()); |
||||||
|
return false; |
||||||
|
} |
||||||
|
// Fine to have this on the stack; this is only called on tasks with large
|
||||||
|
// stacks anyway, due to all the string handling.
|
||||||
|
char buf[kBufSize]; |
||||||
|
Tagctx ctx; |
||||||
|
ctx.read = libtags::read; |
||||||
|
ctx.seek = libtags::seek; |
||||||
|
ctx.tag = libtags::tag; |
||||||
|
ctx.toc = libtags::toc; |
||||||
|
ctx.aux = &aux; |
||||||
|
ctx.buf = buf; |
||||||
|
ctx.bufsz = kBufSize; |
||||||
|
int res = tagsget(&ctx); |
||||||
|
f_close(&aux.file); |
||||||
|
|
||||||
|
if (res != 0) { |
||||||
|
// Parsing failed.
|
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
switch (ctx.format) { |
||||||
|
case Fmp3: |
||||||
|
out->encoding = Encoding::kMp3; |
||||||
|
break; |
||||||
|
default: |
||||||
|
out->encoding = Encoding::kUnsupported; |
||||||
|
} |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
} // namespace database
|
@ -1,4 +1,4 @@ |
|||||||
idf_component_register( |
idf_component_register( |
||||||
SRCS "test_records.cpp" |
SRCS "test_records.cpp" "test_database.cpp" |
||||||
INCLUDE_DIRS "." |
INCLUDE_DIRS "." |
||||||
REQUIRES catch2 cmock database) |
REQUIRES catch2 cmock database drivers fixtures) |
||||||
|
@ -0,0 +1,159 @@ |
|||||||
|
#include "database.hpp" |
||||||
|
|
||||||
|
#include <stdint.h> |
||||||
|
#include <iomanip> |
||||||
|
#include <map> |
||||||
|
#include <memory> |
||||||
|
#include <string> |
||||||
|
|
||||||
|
#include "catch2/catch.hpp" |
||||||
|
#include "driver_cache.hpp" |
||||||
|
#include "esp_log.h" |
||||||
|
#include "file_gatherer.hpp" |
||||||
|
#include "i2c_fixture.hpp" |
||||||
|
#include "leveldb/db.h" |
||||||
|
#include "song.hpp" |
||||||
|
#include "spi_fixture.hpp" |
||||||
|
#include "tag_parser.hpp" |
||||||
|
|
||||||
|
namespace database { |
||||||
|
|
||||||
|
class TestBackends : public IFileGatherer, public ITagParser { |
||||||
|
public: |
||||||
|
std::map<std::string, SongTags> songs; |
||||||
|
|
||||||
|
auto MakeSong(const std::string& path, const std::string& title) -> void { |
||||||
|
SongTags tags; |
||||||
|
tags.encoding = Encoding::kMp3; |
||||||
|
tags.title = title; |
||||||
|
songs[path] = tags; |
||||||
|
} |
||||||
|
|
||||||
|
auto FindFiles(const std::string& root, |
||||||
|
std::function<void(const std::string&)> cb) -> void override { |
||||||
|
for (auto keyval : songs) { |
||||||
|
std::invoke(cb, keyval.first); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
auto ReadAndParseTags(const std::string& path, SongTags* out) |
||||||
|
-> bool override { |
||||||
|
if (songs.contains(path)) { |
||||||
|
*out = songs.at(path); |
||||||
|
return true; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
TEST_CASE("song database", "[integration]") { |
||||||
|
I2CFixture i2c; |
||||||
|
SpiFixture spi; |
||||||
|
drivers::DriverCache drivers; |
||||||
|
auto storage = drivers.AcquireStorage(); |
||||||
|
|
||||||
|
Database::Destroy(); |
||||||
|
|
||||||
|
TestBackends songs; |
||||||
|
auto open_res = Database::Open(&songs, &songs); |
||||||
|
REQUIRE(open_res.has_value()); |
||||||
|
std::unique_ptr<Database> db(open_res.value()); |
||||||
|
|
||||||
|
SECTION("empty database") { |
||||||
|
std::unique_ptr<std::vector<Song>> res(db->GetSongs(10).get().values()); |
||||||
|
REQUIRE(res->size() == 0); |
||||||
|
} |
||||||
|
|
||||||
|
SECTION("add new songs") { |
||||||
|
songs.MakeSong("song1.mp3", "Song 1"); |
||||||
|
songs.MakeSong("song2.wav", "Song 2"); |
||||||
|
songs.MakeSong("song3.exe", "Song 3"); |
||||||
|
|
||||||
|
db->Update(); |
||||||
|
|
||||||
|
std::unique_ptr<std::vector<Song>> res(db->GetSongs(10).get().values()); |
||||||
|
REQUIRE(res->size() == 3); |
||||||
|
CHECK(*res->at(0).tags().title == "Song 1"); |
||||||
|
CHECK(res->at(0).data().id() == 1); |
||||||
|
CHECK(*res->at(1).tags().title == "Song 2"); |
||||||
|
CHECK(res->at(1).data().id() == 2); |
||||||
|
CHECK(*res->at(2).tags().title == "Song 3"); |
||||||
|
CHECK(res->at(2).data().id() == 3); |
||||||
|
|
||||||
|
SECTION("update with no filesystem changes") { |
||||||
|
db->Update(); |
||||||
|
|
||||||
|
std::unique_ptr<std::vector<Song>> new_res( |
||||||
|
db->GetSongs(10).get().values()); |
||||||
|
REQUIRE(new_res->size() == 3); |
||||||
|
CHECK(res->at(0) == new_res->at(0)); |
||||||
|
CHECK(res->at(1) == new_res->at(1)); |
||||||
|
CHECK(res->at(2) == new_res->at(2)); |
||||||
|
} |
||||||
|
|
||||||
|
SECTION("update with all songs gone") { |
||||||
|
songs.songs.clear(); |
||||||
|
|
||||||
|
db->Update(); |
||||||
|
|
||||||
|
std::unique_ptr<std::vector<Song>> new_res( |
||||||
|
db->GetSongs(10).get().values()); |
||||||
|
CHECK(new_res->size() == 0); |
||||||
|
|
||||||
|
SECTION("update with one song returned") { |
||||||
|
songs.MakeSong("song2.wav", "Song 2"); |
||||||
|
|
||||||
|
db->Update(); |
||||||
|
|
||||||
|
std::unique_ptr<std::vector<Song>> new_res( |
||||||
|
db->GetSongs(10).get().values()); |
||||||
|
REQUIRE(new_res->size() == 1); |
||||||
|
CHECK(res->at(1) == new_res->at(0)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
SECTION("update with one song gone") { |
||||||
|
songs.songs.erase("song2.wav"); |
||||||
|
|
||||||
|
db->Update(); |
||||||
|
|
||||||
|
std::unique_ptr<std::vector<Song>> new_res( |
||||||
|
db->GetSongs(10).get().values()); |
||||||
|
REQUIRE(new_res->size() == 2); |
||||||
|
CHECK(res->at(0) == new_res->at(0)); |
||||||
|
CHECK(res->at(2) == new_res->at(1)); |
||||||
|
} |
||||||
|
|
||||||
|
SECTION("update with tags changed") { |
||||||
|
songs.MakeSong("song3.exe", "The Song 3"); |
||||||
|
|
||||||
|
db->Update(); |
||||||
|
|
||||||
|
std::unique_ptr<std::vector<Song>> new_res( |
||||||
|
db->GetSongs(10).get().values()); |
||||||
|
REQUIRE(new_res->size() == 3); |
||||||
|
CHECK(res->at(0) == new_res->at(0)); |
||||||
|
CHECK(res->at(1) == new_res->at(1)); |
||||||
|
CHECK(*new_res->at(2).tags().title == "The Song 3"); |
||||||
|
// The id should not have changed, since this was just a tag update.
|
||||||
|
CHECK(res->at(2).data().id() == new_res->at(2).data().id()); |
||||||
|
} |
||||||
|
|
||||||
|
SECTION("update with one new song") { |
||||||
|
songs.MakeSong("my song.midi", "Song 1 (nightcore remix)"); |
||||||
|
|
||||||
|
db->Update(); |
||||||
|
|
||||||
|
std::unique_ptr<std::vector<Song>> new_res( |
||||||
|
db->GetSongs(10).get().values()); |
||||||
|
REQUIRE(new_res->size() == 4); |
||||||
|
CHECK(res->at(0) == new_res->at(0)); |
||||||
|
CHECK(res->at(1) == new_res->at(1)); |
||||||
|
CHECK(res->at(2) == new_res->at(2)); |
||||||
|
CHECK(*new_res->at(3).tags().title == "Song 1 (nightcore remix)"); |
||||||
|
CHECK(new_res->at(3).data().id() == 4); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} // namespace database
|
@ -1,3 +1,3 @@ |
|||||||
idf_component_register( |
idf_component_register( |
||||||
SRCS "test_storage.cpp" "test_gpio_expander.cpp" "test_battery.cpp" "test_dac.cpp" |
SRCS "test_storage.cpp" "test_gpio_expander.cpp" "test_battery.cpp" "test_dac.cpp" |
||||||
INCLUDE_DIRS "." REQUIRES catch2 cmock drivers) |
INCLUDE_DIRS "." REQUIRES catch2 cmock drivers fixtures) |
||||||
|
@ -0,0 +1 @@ |
|||||||
|
idf_component_register(INCLUDE_DIRS "." REQUIRES drivers) |
Loading…
Reference in new issue