1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <stdbool.h>
5 
6 #include "sd-device.h"
7 
8 #include "hashmap.h"
9 #include "log.h"
10 #include "macro.h"
11 #include "set.h"
12 
13 #define FOREACH_DEVICE_PROPERTY(device, key, value)                \
14         for (key = sd_device_get_property_first(device, &(value)); \
15              key;                                                  \
16              key = sd_device_get_property_next(device, &(value)))
17 
18 #define FOREACH_DEVICE_TAG(device, tag)             \
19         for (tag = sd_device_get_tag_first(device); \
20              tag;                                   \
21              tag = sd_device_get_tag_next(device))
22 
23 #define FOREACH_DEVICE_CURRENT_TAG(device, tag)             \
24         for (tag = sd_device_get_current_tag_first(device); \
25              tag;                                   \
26              tag = sd_device_get_current_tag_next(device))
27 
28 #define FOREACH_DEVICE_SYSATTR(device, attr)             \
29         for (attr = sd_device_get_sysattr_first(device); \
30              attr;                                       \
31              attr = sd_device_get_sysattr_next(device))
32 
33 #define FOREACH_DEVICE_DEVLINK(device, devlink)             \
34         for (devlink = sd_device_get_devlink_first(device); \
35              devlink;                                   \
36              devlink = sd_device_get_devlink_next(device))
37 
38 #define FOREACH_DEVICE(enumerator, device)                               \
39         for (device = sd_device_enumerator_get_device_first(enumerator); \
40              device;                                                     \
41              device = sd_device_enumerator_get_device_next(enumerator))
42 
43 #define FOREACH_SUBSYSTEM(enumerator, device)                               \
44         for (device = sd_device_enumerator_get_subsystem_first(enumerator); \
45              device;                                                        \
46              device = sd_device_enumerator_get_subsystem_next(enumerator))
47 
48 #define log_device_full_errno_zerook(device, level, error, ...)         \
49         ({                                                              \
50                 const char *_sysname = NULL;                            \
51                 sd_device *_d = (device);                               \
52                 int _level = (level), _e = (error);                     \
53                                                                         \
54                 if (_d && _unlikely_(log_get_max_level() >= LOG_PRI(_level))) \
55                         (void) sd_device_get_sysname(_d, &_sysname);    \
56                 log_object_internal(_level, _e, PROJECT_FILE, __LINE__, __func__, \
57                                     _sysname ? "DEVICE=" : NULL, _sysname, \
58                                     NULL, NULL, __VA_ARGS__);           \
59         })
60 
61 #define log_device_full_errno(device, level, error, ...)                \
62         ({                                                              \
63                 int _error = (error);                                   \
64                 ASSERT_NON_ZERO(_error);                                \
65                 log_device_full_errno_zerook(device, level, _error, __VA_ARGS__); \
66         })
67 
68 #define log_device_full(device, level, ...) (void) log_device_full_errno_zerook(device, level, 0, __VA_ARGS__)
69 
70 #define log_device_debug(device, ...)   log_device_full(device, LOG_DEBUG, __VA_ARGS__)
71 #define log_device_info(device, ...)    log_device_full(device, LOG_INFO, __VA_ARGS__)
72 #define log_device_notice(device, ...)  log_device_full(device, LOG_NOTICE, __VA_ARGS__)
73 #define log_device_warning(device, ...) log_device_full(device, LOG_WARNING, __VA_ARGS__)
74 #define log_device_error(device, ...)   log_device_full(device, LOG_ERR, __VA_ARGS__)
75 
76 #define log_device_debug_errno(device, error, ...)   log_device_full_errno(device, LOG_DEBUG, error, __VA_ARGS__)
77 #define log_device_info_errno(device, error, ...)    log_device_full_errno(device, LOG_INFO, error, __VA_ARGS__)
78 #define log_device_notice_errno(device, error, ...)  log_device_full_errno(device, LOG_NOTICE, error, __VA_ARGS__)
79 #define log_device_warning_errno(device, error, ...) log_device_full_errno(device, LOG_WARNING, error, __VA_ARGS__)
80 #define log_device_error_errno(device, error, ...)   log_device_full_errno(device, LOG_ERR, error, __VA_ARGS__)
81 
82 bool device_match_sysattr(sd_device *device, Hashmap *match_sysattr, Hashmap *nomatch_sysattr);
83 bool device_match_parent(sd_device *device, Set *match_parent, Set *nomatch_parent);
84