@@ -89,14 +89,7 @@ enum monitor_state {
MONITOR_STATE_RELEASED, /* Dbus Object removed by app */
};
-struct adv_monitor {
- struct adv_monitor_app *app;
- GDBusProxy *proxy;
- char *path;
-
- enum monitor_state state; /* MONITOR_STATE_* */
- uint16_t monitor_handle; /* Kernel Monitor Handle */
-
+struct rssi_parameters {
int8_t high_rssi; /* High RSSI threshold */
uint16_t high_rssi_timeout; /* High RSSI threshold timeout */
int8_t low_rssi; /* Low RSSI threshold */
@@ -105,6 +98,17 @@ struct adv_monitor {
* Currenly unimplemented in user space.
* Used only to pass data to kernel.
*/
+};
+
+struct adv_monitor {
+ struct adv_monitor_app *app;
+ GDBusProxy *proxy;
+ char *path;
+
+ enum monitor_state state; /* MONITOR_STATE_* */
+ uint16_t monitor_handle; /* Kernel Monitor Handle */
+
+ struct rssi_parameters rssi; /* RSSI parameter for this monitor */
struct queue *devices; /* List of adv_monitor_device objects */
enum monitor_type type; /* MONITOR_TYPE_* */
@@ -158,6 +162,24 @@ const struct adv_monitor_type {
{ },
};
+static void rssi_unset(struct rssi_parameters *rssi)
+{
+ rssi->high_rssi = ADV_MONITOR_UNSET_RSSI;
+ rssi->high_rssi_timeout = ADV_MONITOR_UNSET_TIMEOUT;
+ rssi->low_rssi = ADV_MONITOR_UNSET_RSSI;
+ rssi->low_rssi_timeout = ADV_MONITOR_UNSET_TIMEOUT;
+ rssi->sampling_period = ADV_MONITOR_UNSET_SAMPLING_PERIOD;
+}
+
+static bool rssi_is_unset(const struct rssi_parameters *rssi)
+{
+ return rssi->high_rssi == ADV_MONITOR_UNSET_RSSI &&
+ rssi->low_rssi == ADV_MONITOR_UNSET_RSSI &&
+ rssi->high_rssi_timeout == ADV_MONITOR_UNSET_TIMEOUT &&
+ rssi->low_rssi_timeout == ADV_MONITOR_UNSET_TIMEOUT &&
+ rssi->sampling_period == ADV_MONITOR_UNSET_SAMPLING_PERIOD;
+}
+
/* Replies to an app's D-Bus message and unref it */
static void app_reply_msg(struct adv_monitor_app *app, DBusMessage *reply)
{
@@ -369,11 +391,7 @@ static struct adv_monitor *monitor_new(struct adv_monitor_app *app,
monitor->state = MONITOR_STATE_NEW;
- monitor->high_rssi = ADV_MONITOR_UNSET_RSSI;
- monitor->high_rssi_timeout = ADV_MONITOR_UNSET_TIMEOUT;
- monitor->low_rssi = ADV_MONITOR_UNSET_RSSI;
- monitor->low_rssi_timeout = ADV_MONITOR_UNSET_TIMEOUT;
- monitor->sampling_period = ADV_MONITOR_UNSET_SAMPLING_PERIOD;
+ rssi_unset(&monitor->rssi);
monitor->devices = queue_new();
monitor->type = MONITOR_TYPE_NONE;
@@ -528,19 +546,19 @@ static bool parse_rssi_and_timeout(struct adv_monitor *monitor,
if (sampling_period > ADV_MONITOR_MAX_SAMPLING_PERIOD)
goto failed;
- monitor->high_rssi = h_rssi;
- monitor->low_rssi = l_rssi;
- monitor->high_rssi_timeout = h_rssi_timeout;
- monitor->low_rssi_timeout = l_rssi_timeout;
- monitor->sampling_period = sampling_period;
+ monitor->rssi.high_rssi = h_rssi;
+ monitor->rssi.low_rssi = l_rssi;
+ monitor->rssi.high_rssi_timeout = h_rssi_timeout;
+ monitor->rssi.low_rssi_timeout = l_rssi_timeout;
+ monitor->rssi.sampling_period = sampling_period;
done:
DBG("Adv Monitor at %s initiated with high RSSI threshold %d, high "
"RSSI threshold timeout %d, low RSSI threshold %d, low RSSI "
"threshold timeout %d, sampling period %d", path,
- monitor->high_rssi, monitor->high_rssi_timeout,
- monitor->low_rssi, monitor->low_rssi_timeout,
- monitor->sampling_period);
+ monitor->rssi.high_rssi, monitor->rssi.high_rssi_timeout,
+ monitor->rssi.low_rssi, monitor->rssi.low_rssi_timeout,
+ monitor->rssi.sampling_period);
return true;
@@ -700,15 +718,6 @@ static void add_adv_patterns_monitor_cb(uint8_t status, uint16_t length,
DBG("Adv monitor with handle:0x%04x added", monitor->monitor_handle);
}
-static bool monitor_rssi_is_unset(struct adv_monitor *monitor)
-{
- return monitor->high_rssi == ADV_MONITOR_UNSET_RSSI &&
- monitor->low_rssi == ADV_MONITOR_UNSET_RSSI &&
- monitor->high_rssi_timeout == ADV_MONITOR_UNSET_TIMEOUT &&
- monitor->low_rssi_timeout == ADV_MONITOR_UNSET_TIMEOUT &&
- monitor->sampling_period == ADV_MONITOR_UNSET_SAMPLING_PERIOD;
-}
-
/* sends MGMT_OP_ADD_ADV_PATTERNS_MONITOR */
static bool monitor_send_add_pattern(struct adv_monitor *monitor)
{
@@ -758,12 +767,12 @@ static bool monitor_send_add_pattern_rssi(struct adv_monitor *monitor)
if (!cp)
return false;
- cp->rssi.high_threshold = monitor->high_rssi;
+ cp->rssi.high_threshold = monitor->rssi.high_rssi;
/* High threshold timeout is unsupported in kernel. Value must be 0. */
cp->rssi.high_threshold_timeout = 0;
- cp->rssi.low_threshold = monitor->low_rssi;
- cp->rssi.low_threshold_timeout = htobs(monitor->low_rssi_timeout);
- cp->rssi.sampling_period = monitor->sampling_period;
+ cp->rssi.low_threshold = monitor->rssi.low_rssi;
+ cp->rssi.low_threshold_timeout = htobs(monitor->rssi.low_rssi_timeout);
+ cp->rssi.sampling_period = monitor->rssi.sampling_period;
for (e = queue_get_entries(monitor->patterns); e; e = e->next) {
struct bt_ad_pattern *pattern = e->data;
@@ -822,7 +831,7 @@ static void monitor_proxy_added_cb(GDBusProxy *proxy, void *user_data)
queue_push_tail(app->monitors, monitor);
- if (monitor_rssi_is_unset(monitor))
+ if (rssi_is_unset(&monitor->rssi))
monitor_send_add_pattern(monitor);
else
monitor_send_add_pattern_rssi(monitor);
@@ -1486,7 +1495,7 @@ static bool handle_device_lost_timeout(gpointer user_data)
* If not, report the Device Lost event.
*/
if (difftime(curr_time, dev->last_seen) >=
- monitor->low_rssi_timeout) {
+ monitor->rssi.low_rssi_timeout) {
dev->found = false;
DBG("Calling DeviceLost() on Adv Monitor of owner %s "
@@ -1513,7 +1522,7 @@ static void adv_monitor_filter_rssi(struct adv_monitor *monitor,
* DeviceFound() event without tracking for the RSSI as the Adv has
* already matched the pattern filter.
*/
- if (monitor_rssi_is_unset(monitor)) {
+ if (rssi_is_unset(&monitor->rssi)) {
DBG("Calling DeviceFound() on Adv Monitor of owner %s "
"at path %s", monitor->app->owner, monitor->path);
@@ -1544,22 +1553,22 @@ static void adv_monitor_filter_rssi(struct adv_monitor *monitor,
*/
if (dev->last_seen) {
if (difftime(curr_time, dev->last_seen) >
- monitor->high_rssi_timeout) {
+ monitor->rssi.high_rssi_timeout) {
dev->high_rssi_first_seen = 0;
}
if (difftime(curr_time, dev->last_seen) >
- monitor->low_rssi_timeout) {
+ monitor->rssi.low_rssi_timeout) {
dev->low_rssi_first_seen = 0;
}
}
dev->last_seen = curr_time;
/* Check for the found devices (if the device is not already found) */
- if (!dev->found && rssi > monitor->high_rssi) {
+ if (!dev->found && rssi > monitor->rssi.high_rssi) {
if (dev->high_rssi_first_seen) {
if (difftime(curr_time, dev->high_rssi_first_seen) >=
- monitor->high_rssi_timeout) {
+ monitor->rssi.high_rssi_timeout) {
dev->found = true;
DBG("Calling DeviceFound() on Adv Monitor "
@@ -1582,10 +1591,10 @@ static void adv_monitor_filter_rssi(struct adv_monitor *monitor,
* it doesn't make any sense to report the Device Lost event if the
* device is not found yet)
*/
- if (dev->found && rssi < monitor->low_rssi) {
+ if (dev->found && rssi < monitor->rssi.low_rssi) {
if (dev->low_rssi_first_seen) {
if (difftime(curr_time, dev->low_rssi_first_seen) >=
- monitor->low_rssi_timeout) {
+ monitor->rssi.low_rssi_timeout) {
dev->found = false;
DBG("Calling DeviceLost() on Adv Monitor "
@@ -1610,8 +1619,8 @@ static void adv_monitor_filter_rssi(struct adv_monitor *monitor,
*/
if (dev->found) {
dev->lost_timer =
- timeout_add_seconds(monitor->low_rssi_timeout,
+ timeout_add_seconds(monitor->rssi.low_rssi_timeout,
handle_device_lost_timeout, dev,
- NULL);
+ NULL);
}
}