diff --git a/src/agent.c b/src/agent.c
index 4a2f606..8c1211c 100644
--- a/src/agent.c
+++ b/src/agent.c
#include "adapter.h"
#include "device.h"
#include "agent.h"
+#include "shared/queue.h"
#define IO_CAPABILITY_DISPLAYONLY 0x00
#define IO_CAPABILITY_DISPLAYYESNO 0x01
#define AGENT_INTERFACE "org.bluez.Agent1"
static GHashTable *agent_list;
-static struct agent *default_agent = NULL;
+struct queue *default_agents = NULL;
typedef enum {
AGENT_REQUEST_PASSKEY,
adapter_set_io_capability(adapter, io_cap);
}
-static void set_default_agent(struct agent *agent)
+static bool add_default_agent(struct agent *agent)
{
- if (default_agent == agent)
+ if (queue_peek_head(default_agents) == agent)
+ return true;
+
+ queue_remove(default_agents, agent);
+
+ if (!queue_push_head(default_agents, agent))
+ return false;
+
+ DBG("Default agent set to %s %s", agent->owner, agent->path);
+
+ adapter_foreach(set_io_cap, agent);
+
+ return true;
+}
+
+static void remove_default_agent(struct agent *agent)
+{
+ if (queue_peek_head(default_agents) != agent) {
+ queue_remove(default_agents, agent);
return;
+ }
+
+ queue_remove(default_agents, agent);
+ agent = queue_peek_head(default_agents);
if (agent)
DBG("Default agent set to %s %s", agent->owner, agent->path);
else
DBG("Default agent cleared");
- default_agent = agent;
-
adapter_foreach(set_io_cap, agent);
}
agent->watch = 0;
}
- if (agent == default_agent)
- set_default_agent(NULL);
+ remove_default_agent(agent);
g_hash_table_remove(agent_list, agent->owner);
}
return agent_ref(agent);
}
- if (default_agent)
- return agent_ref(default_agent);
+ if (!queue_isempty(default_agents))
+ return agent_ref(queue_peek_head(default_agents));
return NULL;
}
agent_release(agent);
}
+ remove_default_agent(agent);
+
agent_unref(agent);
}
if (g_str_equal(path, agent->path) == FALSE)
return btd_error_does_not_exist(msg);
- set_default_agent(agent);
+ if (!add_default_agent(agent))
+ return btd_error_failed(msg, "Failed to set as default");
return dbus_message_new_method_return(msg);
}
agent_list = g_hash_table_new_full(g_str_hash, g_str_equal,
NULL, agent_destroy);
+ default_agents = queue_new();
+
g_dbus_register_interface(btd_get_dbus_connection(),
"/org/bluez", "org.bluez.AgentManager1",
methods, NULL, NULL, NULL, NULL);
g_dbus_unregister_interface(btd_get_dbus_connection(),
"/org/bluez", "org.bluez.AgentManager1");
- set_default_agent(NULL);
g_hash_table_destroy(agent_list);
+ queue_destroy(default_agents, NULL);
}