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 fprintf (stderr, "Error sending EntryGroupNew message: %s\n", error.message);
72 dbus_error_free (&error);
74 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
80 fprintf (stderr, "Got NULL reply from EntryGroupNew\n");
82 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
86 dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID);
88 if (dbus_error_is_set (&error))
90 fprintf (stderr, "Failure parsing EntryGroupNew reply: %s\n", error.message);
91 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
95 tmp = malloc (sizeof (AvahiEntryGroup));
99 tmp->path = strdup (path);
100 tmp->callback = callback;
101 tmp->user_data = user_data;
103 AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, client->groups, tmp);
105 dbus_message_unref (message);
107 avahi_client_set_errno (client, AVAHI_OK);
112 if (message) dbus_message_unref (message);
117 avahi_entry_group_free (AvahiEntryGroup *group)
119 AvahiClient *client = group->client;
120 DBusMessage *message;
122 if (group == NULL || group->path == NULL)
123 return avahi_client_set_errno (client, AVAHI_ERR_INVALID_OBJECT);
125 message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
126 group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Free");
129 return avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
131 dbus_connection_send (client->bus, message, NULL);
135 return avahi_client_set_errno (client, AVAHI_OK);
139 avahi_entry_group_commit (AvahiEntryGroup *group)
141 DBusMessage *message;
144 dbus_error_init (&error);
146 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
147 AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Commit");
149 dbus_connection_send (group->client->bus, message, NULL);
151 return avahi_client_set_errno (group->client, AVAHI_OK);
155 avahi_entry_group_reset (AvahiEntryGroup *group)
157 DBusMessage *message;
159 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
160 AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Reset");
162 dbus_connection_send (group->client->bus, message, NULL);
164 return avahi_client_set_errno (group->client, AVAHI_OK);
168 avahi_entry_group_get_state (AvahiEntryGroup *group)
170 DBusMessage *message, *reply;
174 dbus_error_init (&error);
176 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
177 AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState");
179 reply = dbus_connection_send_with_reply_and_block (group->client->bus, message, -1, &error);
181 if (dbus_error_is_set (&error))
183 fprintf (stderr, "Error sending GetState message for %s EntryGroup: %s\n", group->path, error.message);
184 dbus_error_free (&error);
186 return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
189 dbus_message_get_args(message, &error, DBUS_TYPE_BOOLEAN, &state, DBUS_TYPE_INVALID);
191 if (dbus_error_is_set (&error))
193 fprintf (stderr, "Error parsing GetState reply for %s EntryGroup: %s\n", group->path, error.message);
194 dbus_error_free (&error);
196 return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
199 avahi_client_set_errno (group->client, AVAHI_OK);
204 avahi_client_errno (AvahiClient *client)
206 return client->error;
210 avahi_entry_group_get_client (AvahiEntryGroup *group)
212 return group->client;
216 avahi_entry_group_is_empty (AvahiEntryGroup *group)
222 avahi_entry_group_add_service (AvahiEntryGroup *group,
223 AvahiIfIndex interface,
224 AvahiProtocol protocol,
230 AvahiStringList *txt)
232 DBusMessage *message;
233 DBusMessageIter iter, sub;
236 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
237 AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService");
241 dbus_message_unref (message);
242 return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
245 if (!dbus_message_append_args (message, DBUS_TYPE_INT32, &interface, DBUS_TYPE_INT32, &protocol,
246 DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &type, DBUS_TYPE_STRING, &domain,
247 DBUS_TYPE_STRING, &host, DBUS_TYPE_UINT16, &port, DBUS_TYPE_INVALID))
249 dbus_message_unref (message);
250 return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
253 dbus_message_iter_init_append(message, &iter);
254 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING, &sub);
256 /* Assemble the AvahiStringList into an Array of Array of Bytes to send over dbus */
257 for (p = txt; p != NULL; p = p->next) {
258 DBusMessageIter sub2;
259 const guint8 *data = p->text;
261 dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2);
262 dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size);
263 dbus_message_iter_close_container(&sub, &sub2);
266 dbus_message_iter_close_container(&iter, &sub);
268 dbus_connection_send (group->client->bus, message, NULL);
270 return avahi_client_set_errno (group->client, AVAHI_OK);
273 /* XXX: debug function */
274 char* avahi_entry_group_path (AvahiEntryGroup *group)
276 if (group != NULL) return group->path;