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
30 #include <dbus/dbus.h>
32 #include <avahi-client/client.h>
33 #include <avahi-common/dbus.h>
34 #include <avahi-common/llist.h>
35 #include <avahi-common/error.h>
36 #include <avahi-common/malloc.h>
41 void avahi_entry_group_state_change (AvahiEntryGroup *group, int state)
43 if (group == NULL || group->callback == NULL)
46 group->callback (group, state, group->user_data);
50 avahi_entry_group_new (AvahiClient *client, AvahiEntryGroupCallback callback, void *user_data)
52 AvahiEntryGroup *tmp = NULL;
53 DBusMessage *message = NULL, *reply;
60 dbus_error_init (&error);
62 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER,
63 AVAHI_DBUS_INTERFACE_SERVER, "EntryGroupNew");
65 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
67 if (dbus_error_is_set (&error))
69 dbus_error_free (&error);
71 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
78 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
82 dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID);
84 if (dbus_error_is_set (&error))
86 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
90 tmp = avahi_new(AvahiEntryGroup, 1);
94 tmp->path = avahi_strdup (path);
95 tmp->callback = callback;
96 tmp->user_data = user_data;
98 AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, client->groups, tmp);
100 dbus_message_unref (message);
102 avahi_client_set_errno (client, AVAHI_OK);
106 if (tmp) avahi_free (tmp);
107 if (message) dbus_message_unref (message);
112 avahi_entry_group_free (AvahiEntryGroup *group)
114 AvahiClient *client = group->client;
115 DBusMessage *message;
117 if (group == NULL || group->path == NULL)
118 return avahi_client_set_errno (client, AVAHI_ERR_INVALID_OBJECT);
120 message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
121 group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Free");
124 return avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
126 dbus_connection_send (client->bus, message, NULL);
128 AVAHI_LLIST_REMOVE(AvahiEntryGroup, groups, client->groups, group);
132 return avahi_client_set_errno (client, AVAHI_OK);
136 avahi_entry_group_commit (AvahiEntryGroup *group)
138 DBusMessage *message;
141 dbus_error_init (&error);
143 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
144 AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Commit");
146 dbus_connection_send (group->client->bus, message, NULL);
148 return avahi_client_set_errno (group->client, AVAHI_OK);
152 avahi_entry_group_reset (AvahiEntryGroup *group)
154 DBusMessage *message;
156 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
157 AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Reset");
159 dbus_connection_send (group->client->bus, message, NULL);
161 return avahi_client_set_errno (group->client, AVAHI_OK);
165 avahi_entry_group_get_state (AvahiEntryGroup *group)
167 DBusMessage *message, *reply;
171 dbus_error_init (&error);
173 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
174 AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState");
176 reply = dbus_connection_send_with_reply_and_block (group->client->bus, message, -1, &error);
178 if (dbus_error_is_set (&error))
180 dbus_error_free (&error);
182 return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
185 dbus_message_get_args(message, &error, DBUS_TYPE_BOOLEAN, &state, DBUS_TYPE_INVALID);
187 if (dbus_error_is_set (&error))
189 dbus_error_free (&error);
191 return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
194 avahi_client_set_errno (group->client, AVAHI_OK);
199 avahi_client_errno (AvahiClient *client)
201 return client->error;
205 avahi_entry_group_get_client (AvahiEntryGroup *group)
207 return group->client;
211 avahi_entry_group_is_empty (AvahiEntryGroup *group)
217 avahi_entry_group_add_service (AvahiEntryGroup *group,
218 AvahiIfIndex interface,
219 AvahiProtocol protocol,
225 AvahiStringList *txt)
227 DBusMessage *message;
228 DBusMessageIter iter, sub;
231 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
232 AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService");
236 dbus_message_unref (message);
237 return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
240 if (!dbus_message_append_args (message, DBUS_TYPE_INT32, &interface, DBUS_TYPE_INT32, &protocol,
241 DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &type, DBUS_TYPE_STRING, &domain,
242 DBUS_TYPE_STRING, &host, DBUS_TYPE_UINT16, &port, DBUS_TYPE_INVALID))
244 dbus_message_unref (message);
245 return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
248 dbus_message_iter_init_append(message, &iter);
249 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING, &sub);
251 /* Assemble the AvahiStringList into an Array of Array of Bytes to send over dbus */
252 for (p = txt; p != NULL; p = p->next) {
253 DBusMessageIter sub2;
254 const uint8_t *data = p->text;
256 dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2);
257 dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size);
258 dbus_message_iter_close_container(&sub, &sub2);
261 dbus_message_iter_close_container(&iter, &sub);
263 dbus_connection_send (group->client->bus, message, NULL);
265 return avahi_client_set_errno (group->client, AVAHI_OK);
268 /* XXX: debug function */
269 const char* avahi_entry_group_path (AvahiEntryGroup *group)
271 if (group != NULL) return group->path;