xref: /openbmc/phosphor-certificate-manager/watch.hpp (revision 223e460421eebb1c598d9285b0cb01f1150fa50d)
1ffad1ef1SMarri Devender Rao #pragma once
2ffad1ef1SMarri Devender Rao #include <sdeventplus/source/event.hpp>
3ffad1ef1SMarri Devender Rao #include <sdeventplus/source/io.hpp>
4*223e4604SPatrick Williams 
5*223e4604SPatrick Williams #include <functional>
6*223e4604SPatrick Williams #include <memory>
7ebd1d8a8SWilliam A. Kennington III #include <string>
8ebd1d8a8SWilliam A. Kennington III 
9e1289adfSNan Zhou namespace phosphor::certs
10ffad1ef1SMarri Devender Rao {
11ffad1ef1SMarri Devender Rao /** @class Watch
12ffad1ef1SMarri Devender Rao  *
13ffad1ef1SMarri Devender Rao  *  @brief Adds inotify watch on certificate directory
14ffad1ef1SMarri Devender Rao  *
15ffad1ef1SMarri Devender Rao  *  The inotify watch is hooked up with sd-event, so that on call back,
16ffad1ef1SMarri Devender Rao  *  appropriate actions related to a certificate upload can be taken.
17ffad1ef1SMarri Devender Rao  */
18ffad1ef1SMarri Devender Rao class Watch
19ffad1ef1SMarri Devender Rao {
20ffad1ef1SMarri Devender Rao   public:
21ffad1ef1SMarri Devender Rao     using Callback = std::function<void()>;
22ffad1ef1SMarri Devender Rao     /** @brief ctor - hook inotify watch with sd-event
23ffad1ef1SMarri Devender Rao      *
24ffad1ef1SMarri Devender Rao      *  @param[in] loop - sd-event object
25ffad1ef1SMarri Devender Rao      *  @param[in] cb - The callback function for processing
26ffad1ef1SMarri Devender Rao      *                             certificate upload
27ffad1ef1SMarri Devender Rao      */
28ffad1ef1SMarri Devender Rao     Watch(sdeventplus::Event& event, std::string& certFile, Callback cb);
29ffad1ef1SMarri Devender Rao     Watch(const Watch&) = delete;
30ffad1ef1SMarri Devender Rao     Watch& operator=(const Watch&) = delete;
31ffad1ef1SMarri Devender Rao     Watch(Watch&&) = delete;
32ffad1ef1SMarri Devender Rao     Watch& operator=(Watch&&) = delete;
33ffad1ef1SMarri Devender Rao 
34ffad1ef1SMarri Devender Rao     /** @brief dtor - remove inotify watch and close fd's
35ffad1ef1SMarri Devender Rao      */
36ffad1ef1SMarri Devender Rao     ~Watch();
37ffad1ef1SMarri Devender Rao 
38ffad1ef1SMarri Devender Rao     /** @brief start watch on the specified path
39ffad1ef1SMarri Devender Rao      */
40ffad1ef1SMarri Devender Rao     void startWatch();
41ffad1ef1SMarri Devender Rao 
42ffad1ef1SMarri Devender Rao     /** @brief stop watch on the specified path
43ffad1ef1SMarri Devender Rao      */
44ffad1ef1SMarri Devender Rao     void stopWatch();
45ffad1ef1SMarri Devender Rao 
46ffad1ef1SMarri Devender Rao   private:
47ffad1ef1SMarri Devender Rao     /** @brief certificate upload directory watch descriptor */
48ffad1ef1SMarri Devender Rao     int wd = -1;
49ffad1ef1SMarri Devender Rao 
50ffad1ef1SMarri Devender Rao     /** @brief inotify file descriptor */
51ffad1ef1SMarri Devender Rao     int fd = -1;
52ffad1ef1SMarri Devender Rao 
53ffad1ef1SMarri Devender Rao     /** @brief SDEventPlus IO pointer added to event loop */
54ffad1ef1SMarri Devender Rao     std::unique_ptr<sdeventplus::source::IO> ioPtr = nullptr;
55ffad1ef1SMarri Devender Rao 
56ffad1ef1SMarri Devender Rao     /** @brief sd-event object */
57ffad1ef1SMarri Devender Rao     sdeventplus::Event& event;
58ffad1ef1SMarri Devender Rao 
59ffad1ef1SMarri Devender Rao     /** @brief callback method to be called */
60ffad1ef1SMarri Devender Rao     Callback callback;
61ffad1ef1SMarri Devender Rao 
62ffad1ef1SMarri Devender Rao     /** @brief Certificate directory to watch */
63ffad1ef1SMarri Devender Rao     std::string watchDir;
64ffad1ef1SMarri Devender Rao 
65ffad1ef1SMarri Devender Rao     /** @brief Certificate file to watch */
66ffad1ef1SMarri Devender Rao     std::string watchFile;
67ffad1ef1SMarri Devender Rao 
68ffad1ef1SMarri Devender Rao     /** @brief Certificate file with path */
69ffad1ef1SMarri Devender Rao     std::string certFile;
70ffad1ef1SMarri Devender Rao };
71e1289adfSNan Zhou } // namespace phosphor::certs
72