aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCharles Kerr <charles.kerr@canonical.com>2016-03-21 13:40:11 -0500
committerCharles Kerr <charles.kerr@canonical.com>2016-03-21 13:40:11 -0500
commit7a25132c125f6e5e413ad26ea950ae22bee982f5 (patch)
tree1870caed0aec4960053bd9e5125574dbcb48b08c /src
parent45709c48f34e0909c1309dccac1dd3e047f518fb (diff)
downloadayatana-indicator-display-7a25132c125f6e5e413ad26ea950ae22bee982f5.tar.gz
ayatana-indicator-display-7a25132c125f6e5e413ad26ea950ae22bee982f5.tar.bz2
ayatana-indicator-display-7a25132c125f6e5e413ad26ea950ae22bee982f5.zip
if our USB device is disconnected while prompting the user for ADBD, cancel the prompt.
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/adbd-client.cpp1
-rw-r--r--src/main.cpp4
-rw-r--r--src/usb-manager.cpp63
-rw-r--r--src/usb-manager.h11
-rw-r--r--src/usb-monitor.cpp81
-rw-r--r--src/usb-monitor.h52
-rw-r--r--src/usb-snap.cpp1
8 files changed, 192 insertions, 22 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index d3a021b..cdd2384 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -13,6 +13,7 @@ add_library(
indicator.cpp
rotation-lock.cpp
usb-manager.cpp
+ usb-monitor.cpp
usb-snap.cpp
)
diff --git a/src/adbd-client.cpp b/src/adbd-client.cpp
index 4f7d28f..937215e 100644
--- a/src/adbd-client.cpp
+++ b/src/adbd-client.cpp
@@ -45,6 +45,7 @@ public:
{
// tell the worker thread to stop whatever it's doing and exit.
g_cancellable_cancel(m_cancellable);
+ m_pkresponse_cv.notify_one();
m_sleep_cv.notify_one();
m_worker_thread.join();
g_clear_object(&m_cancellable);
diff --git a/src/main.cpp b/src/main.cpp
index 7d6eb5f..27e6bcc 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -20,6 +20,7 @@
#include <src/exporter.h>
#include <src/rotation-lock.h>
#include <src/usb-manager.h>
+#include <src/usb-monitor.h>
#include <glib/gi18n.h> // bindtextdomain()
#include <gio/gio.h>
@@ -59,7 +60,8 @@ main(int /*argc*/, char** /*argv*/)
// even though it doesn't have an indicator component yet
static constexpr char const * ADB_SOCKET_PATH {"/dev/socket/adbd"};
static constexpr char const * PUBLIC_KEYS_FILENAME {"/data/misc/adb/adb_keys"};
- UsbManager usb_manager {ADB_SOCKET_PATH, PUBLIC_KEYS_FILENAME};
+ auto usb_monitor = std::make_shared<GUDevUsbMonitor>();
+ UsbManager usb_manager {ADB_SOCKET_PATH, PUBLIC_KEYS_FILENAME, usb_monitor};
// let's go!
g_main_loop_run(loop);
diff --git a/src/usb-manager.cpp b/src/usb-manager.cpp
index 7f43520..840a04b 100644
--- a/src/usb-manager.cpp
+++ b/src/usb-manager.cpp
@@ -28,39 +28,57 @@
#include <fcntl.h>
#include <unistd.h>
+#include <set>
+
class UsbManager::Impl
{
public:
explicit Impl(
const std::string& socket_path,
- const std::string& public_keys_filename
+ const std::string& public_keys_filename,
+ const std::shared_ptr<UsbMonitor>& usb_monitor
):
m_adbd_client{std::make_shared<GAdbdClient>(socket_path)},
- m_public_keys_filename{public_keys_filename}
+ m_public_keys_filename{public_keys_filename},
+ m_usb_monitor{usb_monitor}
{
- m_adbd_client->on_pk_request().connect([this](const AdbdClient::PKRequest& req){
- auto snap = new UsbSnap(req.fingerprint);
- snap->on_user_response().connect([this,req,snap](AdbdClient::PKResponse response, bool remember_choice){
- g_debug("%s user responded! response %d, remember %d", G_STRLOC, int(response), int(remember_choice));
- req.respond(response);
- if (remember_choice && (response == AdbdClient::PKResponse::ALLOW))
- write_public_key(req.public_key);
- // delete_later
- g_idle_add([](gpointer gsnap){delete static_cast<UsbSnap*>(gsnap); return G_SOURCE_REMOVE;}, snap);
- });
+ m_usb_monitor->on_usb_disconnected().connect([this](const std::string& /*usb_name*/) {
+ m_snap.reset();
});
- }
- ~Impl()
- {
+ m_adbd_client->on_pk_request().connect(
+ [this](const AdbdClient::PKRequest& req){
+
+ m_snap.reset(new UsbSnap(req.fingerprint),
+ [this](UsbSnap* snap){
+ m_snap_connections.clear();
+ delete snap;
+ }
+ );
+
+ m_snap_connections.insert((*m_snap).on_user_response().connect(
+ [this,req](AdbdClient::PKResponse response, bool remember_choice){
+ g_message("%s user responded! response %d, remember %d", G_STRLOC, int(response), int(remember_choice));
+ req.respond(response);
+ g_message("%s", G_STRLOC);
+ if (remember_choice && (response == AdbdClient::PKResponse::ALLOW))
+ write_public_key(req.public_key);
+ g_idle_add([](gpointer gself){static_cast<Impl*>(gself)->m_snap.reset(); return G_SOURCE_REMOVE;}, this);
+ }
+ ));
+ }
+ );
+
}
+ ~Impl() =default;
+
private:
void write_public_key(const std::string& public_key)
{
- g_debug("writing public key '%s' to '%s'", public_key.c_str(), m_public_keys_filename.c_str());
+ g_message("%s writing public key '%s' to '%s'", G_STRLOC, public_key.c_str(), m_public_keys_filename.c_str());
// confirm the directory exists
auto dirname = g_path_get_dirname(m_public_keys_filename.c_str());
@@ -78,12 +96,12 @@ private:
S_IRUSR|S_IWUSR|S_IRGRP
);
if (fd == -1) {
- g_warning("Error opening ADB datafile '%s': %s", m_public_keys_filename.c_str(), g_strerror(errno));
+ g_warning("Error opening ADB datafile: %s", g_strerror(errno));
return;
}
// write the new public key on its own line
- const std::string buf {public_key + '\n'};
+ std::string buf {public_key + '\n'};
if (write(fd, buf.c_str(), buf.size()) == -1)
g_warning("Error writing ADB datafile: %d %s", errno, g_strerror(errno));
close(fd);
@@ -91,6 +109,10 @@ private:
std::shared_ptr<GAdbdClient> m_adbd_client;
const std::string m_public_keys_filename;
+ std::shared_ptr<UsbMonitor> m_usb_monitor;
+
+ std::shared_ptr<UsbSnap> m_snap;
+ std::set<core::ScopedConnection> m_snap_connections;
};
/***
@@ -99,9 +121,10 @@ private:
UsbManager::UsbManager(
const std::string& socket_path,
- const std::string& public_keys_filename
+ const std::string& public_keys_filename,
+ const std::shared_ptr<UsbMonitor>& usb_monitor
):
- impl{new Impl{socket_path, public_keys_filename}}
+ impl{new Impl{socket_path, public_keys_filename, usb_monitor}}
{
}
diff --git a/src/usb-manager.h b/src/usb-manager.h
index ec405c0..960d634 100644
--- a/src/usb-manager.h
+++ b/src/usb-manager.h
@@ -19,6 +19,8 @@
#pragma once
+#include <src/usb-monitor.h>
+
#include <memory>
#include <string>
@@ -28,10 +30,17 @@
class UsbManager
{
public:
- UsbManager(const std::string& socket_path, const std::string& public_key_filename);
+
+ UsbManager(
+ const std::string& socket_path,
+ const std::string& public_key_filename,
+ const std::shared_ptr<UsbMonitor>&
+ );
+
~UsbManager();
protected:
+
class Impl;
std::unique_ptr<Impl> impl;
};
diff --git a/src/usb-monitor.cpp b/src/usb-monitor.cpp
new file mode 100644
index 0000000..5fc5a6d
--- /dev/null
+++ b/src/usb-monitor.cpp
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2016 Canonical Ltd.
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 3, as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranties of
+ * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ * Charles Kerr <charles.kerr@canonical.com>
+ */
+
+#include <src/usb-monitor.h>
+
+#include <glib.h>
+#include <gudev/gudev.h>
+
+class GUDevUsbMonitor::Impl
+{
+public:
+
+ Impl()
+ {
+ const char* subsystems[] = {"android_usb", nullptr};
+ m_udev_client = g_udev_client_new(subsystems);
+ g_signal_connect(m_udev_client, "uevent", G_CALLBACK(on_android_usb_event), this);
+ }
+
+ ~Impl()
+ {
+ g_signal_handlers_disconnect_by_data(m_udev_client, this);
+ g_clear_object(&m_udev_client);
+ }
+
+ core::Signal<const std::string&>& on_usb_disconnected()
+ {
+ return m_on_usb_disconnected;
+ }
+
+private:
+
+ static void on_android_usb_event(GUdevClient*, gchar* action, GUdevDevice* device, gpointer gself)
+ {
+ if (!g_strcmp0(action, "change"))
+ if (!g_strcmp0(g_udev_device_get_property(device, "USB_STATE"), "DISCONNECTED"))
+ static_cast<Impl*>(gself)->m_on_usb_disconnected(g_udev_device_get_name(device));
+ }
+
+ core::Signal<const std::string&> m_on_usb_disconnected;
+
+ GUdevClient* m_udev_client = nullptr;
+};
+
+/***
+****
+***/
+
+UsbMonitor::UsbMonitor() =default;
+
+UsbMonitor::~UsbMonitor() =default;
+
+GUDevUsbMonitor::GUDevUsbMonitor():
+ impl{new Impl{}}
+{
+}
+
+GUDevUsbMonitor::~GUDevUsbMonitor() =default;
+
+core::Signal<const std::string&>&
+GUDevUsbMonitor::on_usb_disconnected()
+{
+ return impl->on_usb_disconnected();
+}
+
diff --git a/src/usb-monitor.h b/src/usb-monitor.h
new file mode 100644
index 0000000..d9be539
--- /dev/null
+++ b/src/usb-monitor.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2016 Canonical Ltd.
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 3, as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranties of
+ * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ * Charles Kerr <charles.kerr@canonical.com>
+ */
+
+#pragma once
+
+#include <core/signal.h>
+
+#include <memory>
+#include <string>
+
+/**
+ * Simple interface that emits signals on USB device state changes
+ */
+class UsbMonitor
+{
+public:
+ UsbMonitor();
+ virtual ~UsbMonitor();
+ virtual core::Signal<const std::string&>& on_usb_disconnected() =0;
+};
+
+/**
+ * Simple GUDev wrapper that notifies on android_usb device state changes
+ */
+class GUDevUsbMonitor: public UsbMonitor
+{
+public:
+ GUDevUsbMonitor();
+ virtual ~GUDevUsbMonitor();
+ core::Signal<const std::string&>& on_usb_disconnected() override;
+
+protected:
+ class Impl;
+ std::unique_ptr<Impl> impl;
+};
+
diff --git a/src/usb-snap.cpp b/src/usb-snap.cpp
index 41c78c6..349d80e 100644
--- a/src/usb-snap.cpp
+++ b/src/usb-snap.cpp
@@ -148,6 +148,7 @@ private:
{
GError* error {};
auto reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION(obus), res, &error);
+g_message("%s got notify response %s", G_STRLOC, g_variant_print(reply, true));
if (error != nullptr) {
if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
g_warning("UsbSnap: Error calling Notify: %s", error->message);