xref: /spdk/lib/scsi/scsi_rpc.c (revision 552e21cce6cccbf833ed9109827e08337377d7ce)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>.
5  *   Copyright (c) Intel Corporation.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include "scsi_internal.h"
36 
37 #include "spdk/rpc.h"
38 #include "spdk/util.h"
39 
40 static void
41 spdk_rpc_get_scsi_devices(struct spdk_jsonrpc_request *request,
42 			  const struct spdk_json_val *params)
43 {
44 	struct spdk_json_write_ctx *w;
45 	struct spdk_scsi_dev *devs = spdk_scsi_dev_get_list();
46 	int i;
47 
48 	if (params != NULL) {
49 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
50 						 "get_scsi_devices requires no parameters");
51 		return;
52 	}
53 
54 	w = spdk_jsonrpc_begin_result(request);
55 	if (w == NULL) {
56 		return;
57 	}
58 
59 	spdk_json_write_array_begin(w);
60 
61 	for (i = 0; i < SPDK_SCSI_MAX_DEVS; i++) {
62 		struct spdk_scsi_dev *dev = &devs[i];
63 
64 		if (!dev->is_allocated) {
65 			continue;
66 		}
67 
68 		spdk_json_write_object_begin(w);
69 
70 		spdk_json_write_name(w, "id");
71 		spdk_json_write_int32(w, dev->id);
72 
73 		spdk_json_write_name(w, "device_name");
74 		spdk_json_write_string(w, dev->name);
75 
76 		spdk_json_write_object_end(w);
77 	}
78 	spdk_json_write_array_end(w);
79 
80 	spdk_jsonrpc_end_result(request, w);
81 }
82 SPDK_RPC_REGISTER("get_scsi_devices", spdk_rpc_get_scsi_devices, SPDK_RPC_RUNTIME)
83