xref: /spdk/include/spdk/notify.h (revision a6dbe3721eb3b5990707fc3e378c95e505dd8ab5)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2018 Intel Corporation.
3  *   All rights reserved.
4  */
5 
6 #ifndef SPDK_NOTIFY_H
7 #define SPDK_NOTIFY_H
8 
9 #include "spdk/stdinc.h"
10 #include "spdk/json.h"
11 #include "spdk/queue.h"
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 /**
18  * Opaque event type.
19  */
20 struct spdk_notify_type;
21 
22 typedef int (*spdk_notify_foreach_type_cb)(const struct spdk_notify_type *type, void *ctx);
23 
24 #define SPDK_NOTIFY_MAX_NAME_SIZE 128
25 #define SPDK_NOTIFY_MAX_CTX_SIZE 128
26 
27 struct spdk_notify_event {
28 	char type[SPDK_NOTIFY_MAX_NAME_SIZE];
29 	char ctx[SPDK_NOTIFY_MAX_CTX_SIZE];
30 };
31 
32 /**
33  * Callback type for event enumeration.
34  *
35  * \param idx Event index
36  * \param event Event data
37  * \param ctx User context
38  * \return Non zero to break iteration.
39  */
40 typedef int (*spdk_notify_foreach_event_cb)(uint64_t idx, const struct spdk_notify_event *event,
41 		void *ctx);
42 
43 /**
44  * Register \c type as new notification type.
45  *
46  * \note This function is thread safe.
47  *
48  * \param type New notification type to register.
49  * \return registered notification type or NULL on failure.
50  */
51 struct spdk_notify_type *spdk_notify_type_register(const char *type);
52 
53 /**
54  * Return name of the notification type.
55  *
56  * \param type Notification type we are talking about.
57  * \return Name of notification type.
58  */
59 const char *spdk_notify_type_get_name(const struct spdk_notify_type *type);
60 
61 /**
62  * Call cb_fn for all event types.
63  *
64  * \note Whole function call is under lock so user callback should not sleep.
65  * \param cb_fn
66  * \param ctx
67  */
68 void spdk_notify_foreach_type(spdk_notify_foreach_type_cb cb_fn, void *ctx);
69 
70 /**
71  * Send given notification.
72  *
73  * \param type Notification type
74  * \param ctx Notification context
75  *
76  * \return Event index.
77  */
78 uint64_t spdk_notify_send(const char *type, const char *ctx);
79 
80 /**
81  * Call cb_fn with events from given range.
82  *
83  * \note Whole function call is under lock so user callback should not sleep.
84  *
85  * \param start_idx First event index
86  * \param cb_fn User callback function. Return non-zero to break iteration.
87  * \param max Maximum number of invocations of user callback function.
88  * \param ctx User context
89  * \return Number of user callback invocations
90  */
91 uint64_t spdk_notify_foreach_event(uint64_t start_idx, uint64_t max,
92 				   spdk_notify_foreach_event_cb cb_fn, void *ctx);
93 
94 #ifdef __cplusplus
95 }
96 #endif
97 
98 #endif /* SPDK_NOTIFY_H */
99