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 #include <dbus/dbus.h>
35 #include <dbus/dbus-glib-lowlevel.h>
42 void avahi_entry_group_state_change (AvahiEntryGroup *group, int state)
44 if (group == NULL || group->callback == NULL)
47 group->callback (group, state, group->user_data);
51 avahi_entry_group_new (AvahiClient *client, AvahiEntryGroupCallback callback, void *user_data)
53 AvahiEntryGroup *tmp = NULL;
54 DBusMessage *message = NULL, *reply;
61 dbus_error_init (&error);
63 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER,
64 AVAHI_DBUS_INTERFACE_SERVER, "EntryGroupNew");
66 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
68 if (dbus_error_is_set (&error))
70 dbus_error_free (&error);
72 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
79 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
83 dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID);
85 if (dbus_error_is_set (&error))
87 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
91 tmp = malloc (sizeof (AvahiEntryGroup));
95 tmp->path = strdup (path);
96 tmp->callback = callback;
97 tmp->user_data = user_data;
99 AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, client->groups, tmp);
101 dbus_message_unref (message);
103 avahi_client_set_errno (client, AVAHI_OK);
108 if (message) dbus_message_unref (message);
113 avahi_entry_group_free (AvahiEntryGroup *group)
115 AvahiClient *client = group->client;
116 DBusMessage *message;
118 if (group == NULL || group->path == NULL)
119 return avahi_client_set_errno (client, AVAHI_ERR_INVALID_OBJECT);
121 message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
122 group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Free");
125 return avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
127 dbus_connection_send (client->bus, message, NULL);
129 AVAHI_LLIST_REMOVE(AvahiEntryGroup, groups, client->groups, group);
133 return avahi_client_set_errno (client, AVAHI_OK);
137 avahi_entry_group_commit (AvahiEntryGroup *group)
139 DBusMessage *message;
142 dbus_error_init (&error);
144 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
145 AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Commit");
147 dbus_connection_send (group->client->bus, message, NULL);
149 return avahi_client_set_errno (group->client, AVAHI_OK);
153 avahi_entry_group_reset (AvahiEntryGroup *group)
155 DBusMessage *message;
157 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
158 AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Reset");
160 dbus_connection_send (group->client->bus, message, NULL);
162 return avahi_client_set_errno (group->client, AVAHI_OK);
166 avahi_entry_group_get_state (AvahiEntryGroup *group)
168 DBusMessage *message, *reply;
172 dbus_error_init (&error);
174 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
175 AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState");
177 reply = dbus_connection_send_with_reply_and_block (group->client->bus, message, -1, &error);
179 if (dbus_error_is_set (&error))
181 dbus_error_free (&error);
183 return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
186 dbus_message_get_args(message, &error, DBUS_TYPE_BOOLEAN, &state, DBUS_TYPE_INVALID);
188 if (dbus_error_is_set (&error))
190 dbus_error_free (&error);
192 return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
195 avahi_client_set_errno (group->client, AVAHI_OK);
200 avahi_client_errno (AvahiClient *client)
202 return client->error;
206 avahi_entry_group_get_client (AvahiEntryGroup *group)
208 return group->client;
212 avahi_entry_group_is_empty (AvahiEntryGroup *group)
218 avahi_entry_group_add_service (AvahiEntryGroup *group,
219 AvahiIfIndex interface,
220 AvahiProtocol protocol,
226 AvahiStringList *txt)
228 DBusMessage *message;
229 DBusMessageIter iter, sub;
232 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
233 AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService");
237 dbus_message_unref (message);
238 return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
241 if (!dbus_message_append_args (message, DBUS_TYPE_INT32, &interface, DBUS_TYPE_INT32, &protocol,
242 DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &type, DBUS_TYPE_STRING, &domain,
243 DBUS_TYPE_STRING, &host, DBUS_TYPE_UINT16, &port, DBUS_TYPE_INVALID))
245 dbus_message_unref (message);
246 return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
249 dbus_message_iter_init_append(message, &iter);
250 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING, &sub);
252 /* Assemble the AvahiStringList into an Array of Array of Bytes to send over dbus */
253 for (p = txt; p != NULL; p = p->next) {
254 DBusMessageIter sub2;
255 const guint8 *data = p->text;
257 dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2);
258 dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size);
259 dbus_message_iter_close_container(&sub, &sub2);
262 dbus_message_iter_close_container(&iter, &sub);
264 dbus_connection_send (group->client->bus, message, NULL);
266 return avahi_client_set_errno (group->client, AVAHI_OK);
269 /* XXX: debug function */
270 char* avahi_entry_group_path (AvahiEntryGroup *group)
272 if (group != NULL) return group->path;