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_commit (AvahiEntryGroup *group)
119 DBusMessage *message;
122 dbus_error_init (&error);
124 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
125 AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Commit");
127 dbus_connection_send (group->client->bus, message, NULL);
129 return avahi_client_set_errno (group->client, AVAHI_OK);
133 avahi_entry_group_reset (AvahiEntryGroup *group)
135 DBusMessage *message;
137 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
138 AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Reset");
140 dbus_connection_send (group->client->bus, message, NULL);
142 return avahi_client_set_errno (group->client, AVAHI_OK);
146 avahi_entry_group_state (AvahiEntryGroup *group)
148 DBusMessage *message, *reply;
152 dbus_error_init (&error);
154 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
155 AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState");
157 reply = dbus_connection_send_with_reply_and_block (group->client->bus, message, -1, &error);
159 if (dbus_error_is_set (&error))
161 fprintf (stderr, "Error sending GetState message for %s EntryGroup: %s\n", group->path, error.message);
162 dbus_error_free (&error);
164 return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
167 dbus_message_get_args(message, &error, DBUS_TYPE_BOOLEAN, &state, DBUS_TYPE_INVALID);
169 if (dbus_error_is_set (&error))
171 fprintf (stderr, "Error parsing GetState reply for %s EntryGroup: %s\n", group->path, error.message);
172 dbus_error_free (&error);
174 return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
177 avahi_client_set_errno (group->client, AVAHI_OK);
182 avahi_client_errno (AvahiClient *client)
184 return client->error;
188 avahi_entry_group_get_client (AvahiEntryGroup *group)
190 return group->client;
194 avahi_entry_group_is_empty (AvahiEntryGroup *group)
200 avahi_entry_group_add_service (AvahiEntryGroup *group,
201 AvahiIfIndex interface,
202 AvahiProtocol protocol,
208 AvahiStringList *txt)
210 DBusMessage *message;
211 DBusMessageIter iter, sub;
214 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
215 AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService");
219 dbus_message_unref (message);
220 return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
223 if (!dbus_message_append_args (message, DBUS_TYPE_INT32, &interface, DBUS_TYPE_INT32, &protocol,
224 DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &type, DBUS_TYPE_STRING, &domain,
225 DBUS_TYPE_STRING, &host, DBUS_TYPE_UINT16, &port, DBUS_TYPE_INVALID))
227 dbus_message_unref (message);
228 return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
231 dbus_message_iter_init_append(message, &iter);
232 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING, &sub);
234 /* Assemble the AvahiStringList into an Array of Array of Bytes to send over dbus */
235 for (p = txt; p != NULL; p = p->next) {
236 DBusMessageIter sub2;
237 const guint8 *data = p->text;
239 dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2);
240 dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size);
241 dbus_message_iter_close_container(&sub, &sub2);
244 dbus_message_iter_close_container(&iter, &sub);
246 dbus_connection_send (group->client->bus, message, NULL);
248 return avahi_client_set_errno (group->client, AVAHI_OK);
251 /* XXX: debug function */
252 char* avahi_entry_group_path (AvahiEntryGroup *group)
254 if (group != NULL) return group->path;