blob: c63f7655e1fda5d16a7ccd0e36f0c803e70fcb80 [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>
6
Deepak Kodihalli059e2332017-04-12 06:40:53 -05007namespace phosphor
8{
9namespace software
10{
11namespace manager
12{
13
14/** @class Watch
15 *
16 * @brief Adds inotify watch on software image upload directory
17 *
18 * The inotify watch is hooked up with sd-event, so that on call back,
19 * appropriate actions related to a software image upload can be taken.
20 */
21class Watch
22{
Adriana Kobylak2285fe02018-02-27 15:36:59 -060023 public:
24 /** @brief ctor - hook inotify watch with sd-event
25 *
26 * @param[in] loop - sd-event object
27 * @param[in] imageCallback - The callback function for processing
28 * the image
29 */
30 Watch(sd_event* loop, std::function<int(std::string&)> imageCallback);
Deepak Kodihalli059e2332017-04-12 06:40:53 -050031
Adriana Kobylak2285fe02018-02-27 15:36:59 -060032 Watch(const Watch&) = delete;
33 Watch& operator=(const Watch&) = delete;
34 Watch(Watch&&) = delete;
35 Watch& operator=(Watch&&) = delete;
Deepak Kodihalli059e2332017-04-12 06:40:53 -050036
Adriana Kobylak2285fe02018-02-27 15:36:59 -060037 /** @brief dtor - remove inotify watch and close fd's
38 */
39 ~Watch();
Deepak Kodihalli059e2332017-04-12 06:40:53 -050040
Adriana Kobylak2285fe02018-02-27 15:36:59 -060041 private:
42 /** @brief sd-event callback
43 *
44 * @param[in] s - event source, floating (unused) in our case
45 * @param[in] fd - inotify fd
46 * @param[in] revents - events that matched for fd
47 * @param[in] userdata - pointer to Watch object
48 * @returns 0 on success, -1 on fail
49 */
50 static int callback(sd_event_source* s, int fd, uint32_t revents,
51 void* userdata);
Deepak Kodihalli059e2332017-04-12 06:40:53 -050052
Adriana Kobylak2285fe02018-02-27 15:36:59 -060053 /** @brief image upload directory watch descriptor */
54 int wd = -1;
Deepak Kodihalli059e2332017-04-12 06:40:53 -050055
Adriana Kobylak2285fe02018-02-27 15:36:59 -060056 /** @brief inotify file descriptor */
57 int fd = -1;
Gunnar Mills3027bba2017-04-27 15:49:03 -050058
Adriana Kobylak2285fe02018-02-27 15:36:59 -060059 /** @brief The callback function for processing the image. */
60 std::function<int(std::string&)> imageCallback;
Deepak Kodihalli059e2332017-04-12 06:40:53 -050061};
62
63} // namespace manager
64} // namespace software
65} // namespace phosphor