From: Daniel Wagner <[email protected]>

---
 elect/device.c |  128 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 126 insertions(+), 2 deletions(-)

diff --git a/elect/device.c b/elect/device.c
index 33091e6..13f2c70 100644
--- a/elect/device.c
+++ b/elect/device.c
@@ -60,7 +60,10 @@ struct elect_device {
        gboolean active;
        struct ipv4_settings settings;
 
+       char *tty;
+
        DBusPendingCall *call;
+       DBusMessage *pending;
 };
 
 const char *__elect_device_get_path(struct elect_device *device)
@@ -189,11 +192,120 @@ static DBusMessage *device_get_properties(DBusConnection 
*conn,
        return reply;
 }
 
+static void bt_disconnect_reply(DBusPendingCall *call, gpointer user_data)
+{
+       struct elect_device *device = user_data;
+       DBusMessage *reply;
+
+       DBG("%p", device);
+
+       reply = dbus_pending_call_steal_reply(call);
+       dbus_message_unref(reply);
+
+       device->call = NULL;
+
+       g_free(device->tty);
+       device->tty = NULL;
+}
+
+static int bt_disconnect(struct elect_device *device)
+{
+       DBusPendingCall *call;
+       int status;
+
+       DBG("%p", device);
+
+       if (device->tty == NULL)
+               return 0;
+
+       status = bluetooth_send_with_reply(device->path,
+                                       BLUEZ_SERIAL_INTERFACE, "Disconnect",
+                                       &call, bt_disconnect_reply,
+                                       device, NULL, DBUS_TIMEOUT,
+                                       DBUS_TYPE_STRING, &device->tty,
+                                       DBUS_TYPE_INVALID);
+       if (status < 0)
+               return -EINVAL;
+
+       device->call = call;
+
+       return -EINPROGRESS;
+}
+
+static void bt_connect_reply(DBusPendingCall *call, gpointer user_data)
+{
+       struct elect_device *device = user_data;
+       DBusMessage *reply;
+       DBusError derr;
+       char *tty;
+
+       DBG("%p", device);
+
+       reply = dbus_pending_call_steal_reply(call);
+
+       device->call = NULL;
+
+       dbus_error_init(&derr);
+       if (dbus_set_error_from_message(&derr, reply)) {
+               DBG("Connection to bt serial returned with error: %s, %s",
+                                               derr.name, derr.message);
+
+               dbus_error_free(&derr);
+               goto done;
+       }
+
+       dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &tty,
+                       DBUS_TYPE_INVALID);
+
+       DBG("%p tty %s", device, tty);
+
+       device->tty = g_strdup(tty);
+
+done:
+       dbus_message_unref(reply);
+
+       if (device->pending != NULL) {
+               DBusMessage *reply;
+
+               if (device->tty != NULL)
+                       reply = dbus_message_new_method_return(device->pending);
+               else
+                       reply = __ofono_error_failed(device->pending);
+
+               __ofono_dbus_pending_reply(&device->pending, reply);
+
+               device->pending = NULL;
+       }
+}
+
+static int bt_connect(struct elect_device *device)
+{
+       DBusPendingCall *call;
+       int status;
+       char *profile = "dun";
+
+       DBG("%p", device);
+
+       status = bluetooth_send_with_reply(device->path,
+                                       BLUEZ_SERIAL_INTERFACE, "Connect",
+                                       &call, bt_connect_reply,
+                                       device, NULL, DBUS_TIMEOUT,
+                                       DBUS_TYPE_STRING, &profile,
+                                       DBUS_TYPE_INVALID);
+       if (status < 0)
+               return -EINVAL;
+
+       device->call = call;
+
+       return -EINPROGRESS;
+}
+
 static DBusMessage *set_property_active(struct elect_device *device,
                                        DBusMessage *msg,
                                        DBusMessageIter *var)
 {
        ofono_bool_t active;
+       int err;
 
        DBG("%p elect_path %s", device, device->elect_path);
 
@@ -202,9 +314,17 @@ static DBusMessage *set_property_active(struct 
elect_device *device,
 
        dbus_message_iter_get_basic(var, &active);
 
-       device->active = active;
+       if (active)
+               err = bt_connect(device);
+       else
+               err = bt_disconnect(device);
 
-       return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
+       if (err != -EINPROGRESS && err < 0)
+               return __ofono_error_failed(msg);
+
+       device->pending = dbus_message_ref(msg);
+
+       return NULL;
 }
 
 static DBusMessage *device_set_property(DBusConnection *conn,
@@ -418,6 +538,9 @@ static void destroy_device(gpointer user)
 {
        struct elect_device *device = user;
 
+       if (device->pending)
+               dbus_message_unref(device->pending);
+
        if (device->call)
                dbus_pending_call_cancel(device->call);
 
@@ -429,6 +552,7 @@ static void destroy_device(gpointer user)
        g_free(device->path);
        g_free(device->address);
        g_free(device->name);
+       g_free(device->tty);
 
        g_free(device);
 }
-- 
1.7.8.110.g4cb5d1

_______________________________________________
ofono mailing list
[email protected]
http://lists.ofono.org/listinfo/ofono

Reply via email to