xref: /spdk/lib/notify/notify_rpc.c (revision 4e8e97c886e47e337dc470ac8c1ffa044d729af0)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 
35 #include "spdk/rpc.h"
36 #include "spdk/string.h"
37 #include "spdk/notify.h"
38 #include "spdk/env.h"
39 #include "spdk/util.h"
40 
41 #include "spdk/log.h"
42 
43 static int
44 notify_get_types_cb(const struct spdk_notify_type *type, void *ctx)
45 {
46 	spdk_json_write_string((struct spdk_json_write_ctx *)ctx, spdk_notify_type_get_name(type));
47 	return 0;
48 }
49 
50 static void
51 rpc_notify_get_types(struct spdk_jsonrpc_request *request,
52 		     const struct spdk_json_val *params)
53 {
54 	struct spdk_json_write_ctx *w;
55 
56 	if (params != NULL) {
57 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
58 						 "No parameters required");
59 		return;
60 	}
61 
62 	w = spdk_jsonrpc_begin_result(request);
63 	spdk_json_write_array_begin(w);
64 	spdk_notify_foreach_type(notify_get_types_cb, w);
65 	spdk_json_write_array_end(w);
66 
67 	spdk_jsonrpc_end_result(request, w);
68 }
69 SPDK_RPC_REGISTER("notify_get_types", rpc_notify_get_types, SPDK_RPC_RUNTIME)
70 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(notify_get_types, get_notification_types)
71 
72 struct rpc_notify_get_notifications {
73 	uint64_t id;
74 	uint64_t max;
75 
76 	struct spdk_json_write_ctx *w;
77 };
78 
79 static const struct spdk_json_object_decoder rpc_notify_get_notifications_decoders[] = {
80 	{"id", offsetof(struct rpc_notify_get_notifications, id), spdk_json_decode_uint64, true},
81 	{"max", offsetof(struct rpc_notify_get_notifications, max), spdk_json_decode_uint64, true},
82 };
83 
84 
85 static int
86 notify_get_notifications_cb(uint64_t id, const struct spdk_notify_event *ev, void *ctx)
87 {
88 	struct rpc_notify_get_notifications *req = ctx;
89 
90 	spdk_json_write_object_begin(req->w);
91 	spdk_json_write_named_string(req->w, "type", ev->type);
92 	spdk_json_write_named_string(req->w, "ctx", ev->ctx);
93 	spdk_json_write_named_uint64(req->w, "id", id);
94 	spdk_json_write_object_end(req->w);
95 	return 0;
96 }
97 
98 static void
99 rpc_notify_get_notifications(struct spdk_jsonrpc_request *request,
100 			     const struct spdk_json_val *params)
101 {
102 	struct rpc_notify_get_notifications req = {0, UINT64_MAX};
103 
104 	if (params &&
105 	    spdk_json_decode_object(params, rpc_notify_get_notifications_decoders,
106 				    SPDK_COUNTOF(rpc_notify_get_notifications_decoders), &req)) {
107 		SPDK_DEBUGLOG(notify_rpc, "spdk_json_decode_object failed\n");
108 
109 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
110 						 spdk_strerror(EINVAL));
111 		return;
112 	}
113 
114 
115 	req.w = spdk_jsonrpc_begin_result(request);
116 
117 	spdk_json_write_array_begin(req.w);
118 	spdk_notify_foreach_event(req.id, req.max, notify_get_notifications_cb, &req);
119 	spdk_json_write_array_end(req.w);
120 
121 	spdk_jsonrpc_end_result(request, req.w);
122 }
123 SPDK_RPC_REGISTER("notify_get_notifications", rpc_notify_get_notifications, SPDK_RPC_RUNTIME)
124 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(notify_get_notifications, get_notifications)
125 
126 SPDK_LOG_REGISTER_COMPONENT(notify_rpc)
127