Tom Joseph | 4d8d577 | 2021-08-17 07:35:05 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <systemd/sd-event.h> |
| 4 | |
| 5 | #include <functional> |
| 6 | #include <string> |
| 7 | |
| 8 | namespace pldm |
| 9 | { |
| 10 | |
| 11 | namespace fw_update |
| 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 | */ |
| 21 | class Watch |
| 22 | { |
| 23 | 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); |
| 31 | |
| 32 | Watch(const Watch&) = delete; |
| 33 | Watch& operator=(const Watch&) = delete; |
| 34 | Watch(Watch&&) = delete; |
| 35 | Watch& operator=(Watch&&) = delete; |
| 36 | |
| 37 | /** @brief dtor - remove inotify watch and close fd's |
| 38 | */ |
| 39 | ~Watch(); |
| 40 | |
| 41 | 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); |
| 52 | |
| 53 | /** @brief image upload directory watch descriptor */ |
| 54 | int wd = -1; |
| 55 | |
| 56 | /** @brief inotify file descriptor */ |
| 57 | int fd = -1; |
| 58 | |
| 59 | /** @brief The callback function for processing the image. */ |
| 60 | std::function<int(std::string&)> imageCallback; |
| 61 | }; |
| 62 | |
| 63 | } // namespace fw_update |
| 64 | } // namespace pldm |