[PATCH 08/16] peer: Call Agent RequestPeerAuthorization on WPS needs

2014-08-06 Thread Tomasz Bursztyka
This call will be raised if only a peer support both WPS pbc and pin
method and is not currently advertizing for pbc, thus the need for the
user to tell which method is necessary to proceed with the connection.
---
 src/peer.c | 78 +++---
 1 file changed, 70 insertions(+), 8 deletions(-)

diff --git a/src/peer.c b/src/peer.c
index feb8e75..7716f67 100644
--- a/src/peer.c
+++ b/src/peer.c
@@ -24,6 +24,7 @@
 #endif
 
 #include 
+#include 
 #include 
 #include 
 
@@ -451,6 +452,69 @@ static void peer_removed(struct connman_peer *peer)
peer_schedule_changed();
 }
 
+static const char *get_dbus_sender(struct connman_peer *peer)
+{
+   if (!peer->pending)
+   return NULL;
+
+   return dbus_message_get_sender(peer->pending);
+}
+
+static enum connman_peer_wps_method check_wpspin(struct connman_peer *peer,
+   const char *wpspin)
+{
+   int len, i;
+
+   if (!wpspin)
+   return CONNMAN_PEER_WPS_PBC;
+
+   len = strlen(wpspin);
+   if (len == 0)
+   return CONNMAN_PEER_WPS_PBC;
+
+   if (len != 8)
+   return CONNMAN_PEER_WPS_UNKNOWN;
+   for (i = 0; i < 8; i++) {
+   if (!isdigit((unsigned char) wpspin[i]))
+   return CONNMAN_PEER_WPS_UNKNOWN;
+   }
+
+   return CONNMAN_PEER_WPS_PIN;
+}
+
+static void request_authorization_cb(struct connman_peer *peer,
+   bool choice_done, const char *wpspin,
+   const char *error, void *user_data)
+{
+   enum connman_peer_wps_method wps_method;
+   int err;
+
+   DBG("RequestInput return, %p", peer);
+
+   if (error) {
+   if (g_strcmp0(error,
+   "net.connman.Agent.Error.Canceled") == 0) {
+   err = -EINVAL;
+   goto out;
+   }
+   }
+
+   if (!choice_done || !peer_driver->connect) {
+   err = -EINVAL;
+   goto out;
+   }
+
+   wps_method = check_wpspin(peer, wpspin);
+
+   err = peer_driver->connect(peer, wps_method, wpspin);
+   if (err == -EINPROGRESS)
+   return;
+
+out:
+   reply_pending(peer, EIO);
+   connman_peer_set_state(peer, CONNMAN_PEER_STATE_IDLE);
+}
+
 static int peer_connect(struct connman_peer *peer)
 {
int err = -ENOTSUP;
@@ -459,6 +523,12 @@ static int peer_connect(struct connman_peer *peer)
err = peer_driver->connect(peer,
CONNMAN_PEER_WPS_UNKNOWN, NULL);
 
+   if (err == -ENOKEY) {
+   err = __connman_agent_request_peer_authorization(peer,
+   request_authorization_cb, true,
+   get_dbus_sender(peer), NULL);
+   }
+
return err;
 }
 
@@ -689,14 +759,6 @@ static void report_error_cb(void *user_context, bool 
retry, void *user_data)
peer->sub_device = NULL;
 }
 
-static const char *get_dbus_sender(struct connman_peer *peer)
-{
-   if (!peer->pending)
-   return NULL;
-
-   return dbus_message_get_sender(peer->pending);
-}
-
 static int manage_peer_error(struct connman_peer *peer)
 {
int err;
-- 
1.8.5.5

___
connman mailing list
connman@connman.net
https://lists.connman.net/mailman/listinfo/connman


[PATCH 08/16] peer: Call Agent RequestPeerAuthorization on WPS needs

2014-08-01 Thread Tomasz Bursztyka
This call will be raised if only a peer support both WPS pbc and pin
method and is not currently advertizing for pbc, thus the need for the
user to tell which method is necessary to proceed with the connection.
---
 src/peer.c | 59 +++
 1 file changed, 51 insertions(+), 8 deletions(-)

diff --git a/src/peer.c b/src/peer.c
index 08fd8e7..f4fa9de 100644
--- a/src/peer.c
+++ b/src/peer.c
@@ -451,6 +451,50 @@ static void peer_removed(struct connman_peer *peer)
peer_schedule_changed();
 }
 
+static const char *get_dbus_sender(struct connman_peer *peer)
+{
+   if (!peer->pending)
+   return NULL;
+
+   return dbus_message_get_sender(peer->pending);
+}
+
+static void request_authorization_cb(struct connman_peer *peer,
+   bool choice_done, const char *wpspin,
+   const char *error, void *user_data)
+{
+   enum connman_peer_wps_method wps_method;
+   int err;
+
+   DBG("RequestInput return, %p", peer);
+
+   if (error) {
+   if (g_strcmp0(error,
+   "net.connman.Agent.Error.Canceled") == 0) {
+   err = -EINVAL;
+   goto out;
+   }
+   }
+
+   if (!choice_done || !peer_driver->connect) {
+   err = -EINVAL;
+   goto out;
+   }
+
+   if (!wpspin)
+   wps_method = CONNMAN_PEER_WPS_PBC;
+   else
+   wps_method = CONNMAN_PEER_WPS_PIN;
+
+   err = peer_driver->connect(peer, wps_method, wpspin);
+   if (err == -EINPROGRESS)
+   return;
+
+out:
+   reply_pending(peer, EIO);
+   connman_peer_set_state(peer, CONNMAN_PEER_STATE_IDLE);
+}
+
 static int peer_connect(struct connman_peer *peer)
 {
int err = -ENOTSUP;
@@ -459,6 +503,12 @@ static int peer_connect(struct connman_peer *peer)
err = peer_driver->connect(peer,
CONNMAN_PEER_WPS_UNKNOWN, NULL);
 
+   if (err == -ENOKEY) {
+   err = __connman_agent_request_peer_authorization(peer,
+   request_authorization_cb, true,
+   get_dbus_sender(peer), NULL);
+   }
+
return err;
 }
 
@@ -466,6 +516,7 @@ static int peer_disconnect(struct connman_peer *peer)
 {
int err = -ENOTSUP;
 
+   connman_agent_cancel(peer);
reply_pending(peer, ECONNABORTED);
 
if (peer->connection_master)
@@ -683,14 +734,6 @@ static void report_error_cb(void *user_context, bool 
retry, void *user_data)
peer->sub_device = NULL;
 }
 
-static const char *get_dbus_sender(struct connman_peer *peer)
-{
-   if (!peer->pending)
-   return NULL;
-
-   return dbus_message_get_sender(peer->pending);
-}
-
 static int manage_peer_error(struct connman_peer *peer)
 {
int err;
-- 
1.8.5.5

___
connman mailing list
connman@connman.net
https://lists.connman.net/mailman/listinfo/connman