4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <avahi-client/client.h>
27 #include <avahi-common/dbus.h>
28 #include <avahi-common/llist.h>
29 #include <avahi-common/error.h>
34 #define DBUS_API_SUBJECT_TO_CHANGE
35 #include <dbus/dbus.h>
36 #include <dbus/dbus-glib-lowlevel.h>
43 void avahi_entry_group_state_change (AvahiEntryGroup *group, int state)
45 if (group == NULL || group->callback == NULL)
48 group->callback (group, state, group->user_data);
52 avahi_entry_group_new (AvahiClient *client, AvahiEntryGroupCallback callback, void *user_data)
54 AvahiEntryGroup *tmp = NULL;
55 DBusMessage *message = NULL, *reply;
62 dbus_error_init (&error);
64 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER,
65 AVAHI_DBUS_INTERFACE_SERVER, "EntryGroupNew");
67 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
69 if (dbus_error_is_set (&error))
71 dbus_error_free (&error);
73 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
80 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
84 dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID);
86 if (dbus_error_is_set (&error))
88 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
92 tmp = malloc (sizeof (AvahiEntryGroup));
96 tmp->path = strdup (path);
97 tmp->callback = callback;
98 tmp->user_data = user_data;
100 AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, client->groups, tmp);
102 dbus_message_unref (message);
104 avahi_client_set_errno (client, AVAHI_OK);
109 if (message) dbus_message_unref (message);
114 avahi_entry_group_free (AvahiEntryGroup *group)
116 AvahiClient *client = group->client;
117 DBusMessage *message;
119 if (group == NULL || group->path == NULL)
120 return avahi_client_set_errno (client, AVAHI_ERR_INVALID_OBJECT);
122 message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
123 group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Free");
126 return avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
128 dbus_connection_send (client->bus, message, NULL);
130 AVAHI_LLIST_REMOVE(AvahiEntryGroup, groups, client->groups, group);
134 return avahi_client_set_errno (client, AVAHI_OK);
138 avahi_entry_group_commit (AvahiEntryGroup *group)
140 DBusMessage *message;
143 dbus_error_init (&error);
145 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
146 AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Commit");
148 dbus_connection_send (group->client->bus, message, NULL);
150 return avahi_client_set_errno (group->client, AVAHI_OK);
154 avahi_entry_group_reset (AvahiEntryGroup *group)
156 DBusMessage *message;
158 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
159 AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Reset");
161 dbus_connection_send (group->client->bus, message, NULL);
163 return avahi_client_set_errno (group->client, AVAHI_OK);
167 avahi_entry_group_get_state (AvahiEntryGroup *group)
169 DBusMessage *message, *reply;
173 dbus_error_init (&error);
175 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
176 AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState");
178 reply = dbus_connection_send_with_reply_and_block (group->client->bus, message, -1, &error);
180 if (dbus_error_is_set (&error))
182 dbus_error_free (&error);
184 return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
187 dbus_message_get_args(message, &error, DBUS_TYPE_BOOLEAN, &state, DBUS_TYPE_INVALID);
189 if (dbus_error_is_set (&error))
191 dbus_error_free (&error);
193 return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
196 avahi_client_set_errno (group->client, AVAHI_OK);
201 avahi_client_errno (AvahiClient *client)
203 return client->error;
207 avahi_entry_group_get_client (AvahiEntryGroup *group)
209 return group->client;
213 avahi_entry_group_is_empty (AvahiEntryGroup *group)
219 avahi_entry_group_add_service (AvahiEntryGroup *group,
220 AvahiIfIndex interface,
221 AvahiProtocol protocol,
227 AvahiStringList *txt)
229 DBusMessage *message;
230 DBusMessageIter iter, sub;
233 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
234 AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService");
238 dbus_message_unref (message);
239 return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
242 if (!dbus_message_append_args (message, DBUS_TYPE_INT32, &interface, DBUS_TYPE_INT32, &protocol,
243 DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &type, DBUS_TYPE_STRING, &domain,
244 DBUS_TYPE_STRING, &host, DBUS_TYPE_UINT16, &port, DBUS_TYPE_INVALID))
246 dbus_message_unref (message);
247 return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
250 dbus_message_iter_init_append(message, &iter);
251 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING, &sub);
253 /* Assemble the AvahiStringList into an Array of Array of Bytes to send over dbus */
254 for (p = txt; p != NULL; p = p->next) {
255 DBusMessageIter sub2;
256 const guint8 *data = p->text;
258 dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2);
259 dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size);
260 dbus_message_iter_close_container(&sub, &sub2);
263 dbus_message_iter_close_container(&iter, &sub);
265 dbus_connection_send (group->client->bus, message, NULL);
267 return avahi_client_set_errno (group->client, AVAHI_OK);
270 /* XXX: debug function */
271 char* avahi_entry_group_path (AvahiEntryGroup *group)
273 if (group != NULL) return group->path;