blob: 27d2769f7e68329fae8101ea6dd48acc7e57bf1e [file] [log] [blame]
Deepak Kodihalli059e2332017-04-12 06:40:53 -05001#pragma once
2
3#include <systemd/sd-event.h>
4
Gunnar Millsb0ce9962018-09-07 13:39:10 -05005#include <functional>
Andrew Geissler9155b712020-05-16 13:04:44 -05006#include <string>
Gunnar Millsb0ce9962018-09-07 13:39:10 -05007
Deepak Kodihalli059e2332017-04-12 06:40:53 -05008namespace phosphor
9{
10namespace software
11{
12namespace manager
13{
14
15/** @class Watch
16 *
17 * @brief Adds inotify watch on software image upload directory
18 *
19 * The inotify watch is hooked up with sd-event, so that on call back,
20 * appropriate actions related to a software image upload can be taken.
21 */
22class Watch
23{
Adriana Kobylak2285fe02018-02-27 15:36:59 -060024 public:
25 /** @brief ctor - hook inotify watch with sd-event
26 *
27 * @param[in] loop - sd-event object
28 * @param[in] imageCallback - The callback function for processing
29 * the image
30 */
31 Watch(sd_event* loop, std::function<int(std::string&)> imageCallback);
Deepak Kodihalli059e2332017-04-12 06:40:53 -050032
Adriana Kobylak2285fe02018-02-27 15:36:59 -060033 Watch(const Watch&) = delete;
34 Watch& operator=(const Watch&) = delete;
35 Watch(Watch&&) = delete;
36 Watch& operator=(Watch&&) = delete;
Deepak Kodihalli059e2332017-04-12 06:40:53 -050037
Adriana Kobylak2285fe02018-02-27 15:36:59 -060038 /** @brief dtor - remove inotify watch and close fd's
39 */
40 ~Watch();
Deepak Kodihalli059e2332017-04-12 06:40:53 -050041
Adriana Kobylak2285fe02018-02-27 15:36:59 -060042 private:
43 /** @brief sd-event callback
44 *
45 * @param[in] s - event source, floating (unused) in our case
46 * @param[in] fd - inotify fd
47 * @param[in] revents - events that matched for fd
48 * @param[in] userdata - pointer to Watch object
49 * @returns 0 on success, -1 on fail
50 */
51 static int callback(sd_event_source* s, int fd, uint32_t revents,
52 void* userdata);
Deepak Kodihalli059e2332017-04-12 06:40:53 -050053
Adriana Kobylak2285fe02018-02-27 15:36:59 -060054 /** @brief image upload directory watch descriptor */
55 int wd = -1;
Deepak Kodihalli059e2332017-04-12 06:40:53 -050056
Adriana Kobylak2285fe02018-02-27 15:36:59 -060057 /** @brief inotify file descriptor */
58 int fd = -1;
Gunnar Mills3027bba2017-04-27 15:49:03 -050059
Adriana Kobylak2285fe02018-02-27 15:36:59 -060060 /** @brief The callback function for processing the image. */
61 std::function<int(std::string&)> imageCallback;
Deepak Kodihalli059e2332017-04-12 06:40:53 -050062};
63
64} // namespace manager
65} // namespace software
66} // namespace phosphor