xref: /spdk/lib/notify/notify_rpc.c (revision 307b8c112ffd90a26d53dd15fad67bd9038ef526)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (c) Intel Corporation.
3  *   All rights reserved.
4  */
5 
6 
7 #include "spdk/rpc.h"
8 #include "spdk/string.h"
9 #include "spdk/notify.h"
10 #include "spdk/env.h"
11 #include "spdk/util.h"
12 
13 #include "spdk/log.h"
14 
15 static int
16 notify_get_types_cb(const struct spdk_notify_type *type, void *ctx)
17 {
18 	spdk_json_write_string((struct spdk_json_write_ctx *)ctx, spdk_notify_type_get_name(type));
19 	return 0;
20 }
21 
22 static void
23 rpc_notify_get_types(struct spdk_jsonrpc_request *request,
24 		     const struct spdk_json_val *params)
25 {
26 	struct spdk_json_write_ctx *w;
27 
28 	if (params != NULL) {
29 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
30 						 "No parameters required");
31 		return;
32 	}
33 
34 	w = spdk_jsonrpc_begin_result(request);
35 	spdk_json_write_array_begin(w);
36 	spdk_notify_foreach_type(notify_get_types_cb, w);
37 	spdk_json_write_array_end(w);
38 
39 	spdk_jsonrpc_end_result(request, w);
40 }
41 SPDK_RPC_REGISTER("notify_get_types", rpc_notify_get_types, SPDK_RPC_RUNTIME)
42 
43 struct rpc_notify_get_notifications {
44 	uint64_t id;
45 	uint64_t max;
46 
47 	struct spdk_json_write_ctx *w;
48 };
49 
50 static const struct spdk_json_object_decoder rpc_notify_get_notifications_decoders[] = {
51 	{"id", offsetof(struct rpc_notify_get_notifications, id), spdk_json_decode_uint64, true},
52 	{"max", offsetof(struct rpc_notify_get_notifications, max), spdk_json_decode_uint64, true},
53 };
54 
55 
56 static int
57 notify_get_notifications_cb(uint64_t id, const struct spdk_notify_event *ev, void *ctx)
58 {
59 	struct rpc_notify_get_notifications *req = ctx;
60 
61 	spdk_json_write_object_begin(req->w);
62 	spdk_json_write_named_string(req->w, "type", ev->type);
63 	spdk_json_write_named_string(req->w, "ctx", ev->ctx);
64 	spdk_json_write_named_uint64(req->w, "id", id);
65 	spdk_json_write_object_end(req->w);
66 	return 0;
67 }
68 
69 static void
70 rpc_notify_get_notifications(struct spdk_jsonrpc_request *request,
71 			     const struct spdk_json_val *params)
72 {
73 	struct rpc_notify_get_notifications req = {0, UINT64_MAX};
74 
75 	if (params &&
76 	    spdk_json_decode_object(params, rpc_notify_get_notifications_decoders,
77 				    SPDK_COUNTOF(rpc_notify_get_notifications_decoders), &req)) {
78 		SPDK_DEBUGLOG(notify_rpc, "spdk_json_decode_object failed\n");
79 
80 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
81 						 spdk_strerror(EINVAL));
82 		return;
83 	}
84 
85 
86 	req.w = spdk_jsonrpc_begin_result(request);
87 
88 	spdk_json_write_array_begin(req.w);
89 	spdk_notify_foreach_event(req.id, req.max, notify_get_notifications_cb, &req);
90 	spdk_json_write_array_end(req.w);
91 
92 	spdk_jsonrpc_end_result(request, req.w);
93 }
94 SPDK_RPC_REGISTER("notify_get_notifications", rpc_notify_get_notifications, SPDK_RPC_RUNTIME)
95 
96 SPDK_LOG_REGISTER_COMPONENT(notify_rpc)
97