1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2009, 2010 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains interface to the D-Bus graph dict interface helpers
9 **************************************************************************
11 * LADI Session Handler is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * LADI Session Handler is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 * or write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "graph_dict.h"
28 #include "../dbus/error.h"
29 #include "../dbus_constants.h"
33 #define graph_handle ((ladish_graph_handle)call_ptr->iface_context)
35 bool find_dict(struct dbus_method_call
* call_ptr
, uint32_t object_type
, uint64_t object_id
, ladish_dict_handle
* dict_handle_ptr
)
37 ladish_client_handle client
;
38 ladish_port_handle port
;
39 ladish_dict_handle dict
;
43 case GRAPH_DICT_OBJECT_TYPE_GRAPH
:
44 *dict_handle_ptr
= ladish_graph_get_dict(graph_handle
);
46 case GRAPH_DICT_OBJECT_TYPE_CLIENT
:
47 client
= ladish_graph_find_client_by_id(graph_handle
, object_id
);
50 lash_dbus_error(call_ptr
, LASH_DBUS_ERROR_INVALID_ARGS
, "cannot find client %"PRIu64
".", object_id
);
53 *dict_handle_ptr
= ladish_client_get_dict(client
);
55 case GRAPH_DICT_OBJECT_TYPE_PORT
:
56 port
= ladish_graph_find_port_by_id(graph_handle
, object_id
);
59 lash_dbus_error(call_ptr
, LASH_DBUS_ERROR_INVALID_ARGS
, "cannot find port %"PRIu64
".", object_id
);
62 *dict_handle_ptr
= ladish_port_get_dict(port
);
64 case GRAPH_DICT_OBJECT_TYPE_CONNECTION
:
65 dict
= ladish_graph_get_connection_dict(graph_handle
, object_id
);
68 lash_dbus_error(call_ptr
, LASH_DBUS_ERROR_INVALID_ARGS
, "cannot find connection %"PRIu64
".", object_id
);
71 *dict_handle_ptr
= dict
;
75 lash_dbus_error(call_ptr
, LASH_DBUS_ERROR_INVALID_ARGS
, "find_dict() not implemented for object type %"PRIu32
".", object_type
);
81 void ladish_dict_set_dbus(struct dbus_method_call
* call_ptr
)
85 ladish_dict_handle dict
;
89 if (!dbus_message_get_args(
92 DBUS_TYPE_UINT32
, &object_type
,
93 DBUS_TYPE_UINT64
, &object_id
,
94 DBUS_TYPE_STRING
, &key
,
95 DBUS_TYPE_STRING
, &value
,
98 lash_dbus_error(call_ptr
, LASH_DBUS_ERROR_INVALID_ARGS
, "Invalid arguments to method \"%s\": %s", call_ptr
->method_name
, g_dbus_error
.message
);
99 dbus_error_free(&g_dbus_error
);
103 if (!find_dict(call_ptr
, object_type
, object_id
, &dict
))
108 log_info("%s <- %s", key
, value
);
110 if (!ladish_dict_set(dict
, key
, value
))
112 lash_dbus_error(call_ptr
, LASH_DBUS_ERROR_GENERIC
, "ladish_dict_set(\"%s\", \"%s\") failed.");
116 method_return_new_void(call_ptr
);
119 void ladish_dict_get_dbus(struct dbus_method_call
* call_ptr
)
121 uint32_t object_type
;
123 ladish_dict_handle dict
;
127 if (!dbus_message_get_args(
130 DBUS_TYPE_UINT32
, &object_type
,
131 DBUS_TYPE_UINT64
, &object_id
,
132 DBUS_TYPE_STRING
, &key
,
135 lash_dbus_error(call_ptr
, LASH_DBUS_ERROR_INVALID_ARGS
, "Invalid arguments to method \"%s\": %s", call_ptr
->method_name
, g_dbus_error
.message
);
136 dbus_error_free(&g_dbus_error
);
140 if (!find_dict(call_ptr
, object_type
, object_id
, &dict
))
145 value
= ladish_dict_get(dict
, key
);
148 //lash_dbus_error(call_ptr, LASH_DBUS_ERROR_KEY_NOT_FOUND, "Key '%s' not found", key);
152 method_return_new_single(call_ptr
, DBUS_TYPE_STRING
, &value
);
155 void ladish_dict_drop_dbus(struct dbus_method_call
* call_ptr
)
157 uint32_t object_type
;
159 ladish_dict_handle dict
;
162 if (!dbus_message_get_args(
165 DBUS_TYPE_UINT32
, &object_type
,
166 DBUS_TYPE_UINT64
, &object_id
,
167 DBUS_TYPE_STRING
, &key
,
170 lash_dbus_error(call_ptr
, LASH_DBUS_ERROR_INVALID_ARGS
, "Invalid arguments to method \"%s\": %s", call_ptr
->method_name
, g_dbus_error
.message
);
171 dbus_error_free(&g_dbus_error
);
175 if (!find_dict(call_ptr
, object_type
, object_id
, &dict
))
180 ladish_dict_drop(dict
, key
);
182 method_return_new_void(call_ptr
);
185 METHOD_ARGS_BEGIN(Set
, "Set value for specified key")
186 METHOD_ARG_DESCRIBE_IN("object_type", "u", "Type of object, 0 - graph, 1 - client, 2 - port, 3 - connection")
187 METHOD_ARG_DESCRIBE_IN("object_id", "t", "ID of the object")
188 METHOD_ARG_DESCRIBE_IN("key", "s", "Key to set")
189 METHOD_ARG_DESCRIBE_IN("value", "s", "Value to set")
192 METHOD_ARGS_BEGIN(Get
, "Get value for specified key")
193 METHOD_ARG_DESCRIBE_IN("object_type", "u", "Type of object, 0 - graph, 1 - client, 2 - port, 3 - connection")
194 METHOD_ARG_DESCRIBE_IN("object_id", "t", "ID of the object")
195 METHOD_ARG_DESCRIBE_IN("key", "s", "Key to query")
196 METHOD_ARG_DESCRIBE_OUT("value", "s", "Value associated with the key")
199 METHOD_ARGS_BEGIN(Drop
, "Drop entry, if key exists")
200 METHOD_ARG_DESCRIBE_IN("object_type", "u", "Type of object, 0 - graph, 1 - client, 2 - port, 3 - connection")
201 METHOD_ARG_DESCRIBE_IN("object_id", "t", "ID of the object")
202 METHOD_ARG_DESCRIBE_IN("key", "s", "Key of the entry to drop")
206 METHOD_DESCRIBE(Set
, ladish_dict_set_dbus
)
207 METHOD_DESCRIBE(Get
, ladish_dict_get_dbus
)
208 METHOD_DESCRIBE(Drop
, ladish_dict_drop_dbus
)
211 INTERFACE_BEGIN(g_iface_graph_dict
, IFACE_GRAPH_DICT
)
212 INTERFACE_DEFAULT_HANDLER
213 INTERFACE_EXPOSE_METHODS