~aleteoryx/muditaos

ref: 1a6ea1f4b3eb85b3c3018ecf51016cb35d5172cb muditaos/module-vfs/vfsNotifier.hpp -rw-r--r-- 3.7 KiB
1a6ea1f4 — Alek Rudnik [EGD-4205] mudita 1st audio assets (#940) 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
//
#pragma once
#include "ff_stdio.h"
#include <unordered_map>
#include <mutex.hpp>
#include <functional>
#include <string>
#include <thread.hpp>

namespace vfsn::utility
{
    class vfsNotifier
    {
        using FILE = FF_FILE;

      public:
        //! Default constructor and destructor
        vfsNotifier()                    = default;
        vfsNotifier(const vfsNotifier &) = delete;
        vfsNotifier &operator=(const vfsNotifier &) = delete;
        //! Event type notified for indexer
        enum class FsEvent
        {
            initialized, //! Filesystem is intialized
            modified,    //! File is modified
            deleted,     //! File is deleted
            renamed
        };
        /** Notification handler type
         * Notify function callback defiinition
         */
        using NotifyHandler = std::function<void(std::string_view, FsEvent, std::string_view)>;
        /** This method is called by the vfs layer when vfs open file
         * @param[in] filename Opened file path
         * @param[in] mode Open mode
         * @param[in] Handle to opened file
         * @return None
         */
        auto onFileOpen(const char *filename, const char *mode, const FILE *file) noexcept -> void;
        /** This method is called by the vfs layer when vfs close file
         * @param[in] filee Closed file handle
         * @return None
         */
        auto onFileClose(const FILE *file) noexcept -> void;
        /** This method is called by the vfs layer when file is removed
         * @param[in] filename Removed file path
         * @return None
         */
        auto onFileRemove(std::string_view filename) noexcept -> void;
        /** This method is called by the vfs layer when the vfs rename file
         * @param[in] new_file New path name for the file
         * @param[in] old_file Old path for the file
         * @return None
         */
        auto onFileRename(std::string_view new_file, std::string_view old_file) noexcept -> void;
        /** This method is called by the vfs layer when the disk is intiialized
         * @return None
         */
        auto onFileSystemInitialized() noexcept -> void
        {
            notify("/", FsEvent::initialized);
        }
        /** Method for register notification handler
         * @note This function is called from the thread contest which use vfs call
         * @param[in] hwnd Notification handler funtion
         * @return None
         */
        auto registerNotificationHandler(NotifyHandler hwnd) -> void
        {
            notificationCallback = hwnd;
            threadHandle         = hwnd ? cpp_freertos::Thread::GetCurrentThreadHandle() : nullptr;
        }

      private:
        /** Private notification helper internal method
         * @param[in] file Modified file path
         * @param[in] event Notification event type
         * @param[in] old_file Old file path
         * @return None
         */
        auto notify(std::string_view file, FsEvent event, std::string_view old_file = "") -> void
        {
            if (threadHandle != cpp_freertos::Thread::GetCurrentThreadHandle() && notificationCallback)
                notificationCallback(file, event, old_file);
        }

      private:
        //! Map for opened handles and paths
        std::unordered_map<const FILE *, std::string> mOpenedMap;
        //! Mutex for unordered map
        cpp_freertos::MutexStandard mMutex;
        //! Notification handler callback
        NotifyHandler notificationCallback;
        TaskHandle_t threadHandle;
    };
} // namespace vfsn::utility