Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / src / p2p / p2p_dev_disc.c
index 7bd080a..98805fe 100644 (file)
@@ -2,14 +2,8 @@
  * Wi-Fi Direct - P2P Device Discoverability procedure
  * Copyright (c) 2010, Atheros Communications
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- *
- * Alternatively, this software may be distributed under the terms of BSD
- * license.
- *
- * See README and COPYING for more details.
+ * This software may be distributed under the terms of the BSD license.
+ * See README for more details.
  */
 
 #include "includes.h"
@@ -38,7 +32,7 @@ static struct wpabuf * p2p_build_dev_disc_req(struct p2p_data *p2p,
 
        len = p2p_buf_add_ie_hdr(buf);
        p2p_buf_add_device_id(buf, dev_id);
-       p2p_buf_add_group_id(buf, go->p2p_device_addr, go->oper_ssid,
+       p2p_buf_add_group_id(buf, go->info.p2p_device_addr, go->oper_ssid,
                             go->oper_ssid_len);
        p2p_buf_update_ie_hdr(buf, len);
 
@@ -48,8 +42,7 @@ static struct wpabuf * p2p_build_dev_disc_req(struct p2p_data *p2p,
 
 void p2p_dev_disc_req_cb(struct p2p_data *p2p, int success)
 {
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Device Discoverability Request TX callback: success=%d",
+       p2p_dbg(p2p, "Device Discoverability Request TX callback: success=%d",
                success);
 
        if (!success) {
@@ -62,9 +55,7 @@ void p2p_dev_disc_req_cb(struct p2p_data *p2p, int success)
                return;
        }
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: GO acknowledged Device Discoverability Request - wait "
-               "for response");
+       p2p_dbg(p2p, "GO acknowledged Device Discoverability Request - wait for response");
        /*
         * TODO: is the remain-on-channel from Action frame TX long enough for
         * most cases or should we try to increase its duration and/or start
@@ -77,35 +68,34 @@ int p2p_send_dev_disc_req(struct p2p_data *p2p, struct p2p_device *dev)
 {
        struct p2p_device *go;
        struct wpabuf *req;
+       unsigned int wait_time;
 
        go = p2p_get_device(p2p, dev->member_in_go_dev);
        if (go == NULL || dev->oper_freq <= 0) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Could not find peer entry for GO and frequency "
-                       "to send Device Discoverability Request");
+               p2p_dbg(p2p, "Could not find peer entry for GO and frequency to send Device Discoverability Request");
                return -1;
        }
 
-       req = p2p_build_dev_disc_req(p2p, go, dev->p2p_device_addr);
+       req = p2p_build_dev_disc_req(p2p, go, dev->info.p2p_device_addr);
        if (req == NULL)
                return -1;
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Sending Device Discoverability Request to GO " MACSTR
+       p2p_dbg(p2p, "Sending Device Discoverability Request to GO " MACSTR
                " for client " MACSTR,
-               MAC2STR(go->p2p_device_addr), MAC2STR(dev->p2p_device_addr));
+               MAC2STR(go->info.p2p_device_addr),
+               MAC2STR(dev->info.p2p_device_addr));
 
        p2p->pending_client_disc_go = go;
-       os_memcpy(p2p->pending_client_disc_addr, dev->p2p_device_addr,
+       os_memcpy(p2p->pending_client_disc_addr, dev->info.p2p_device_addr,
                  ETH_ALEN);
        p2p->pending_action_state = P2P_PENDING_DEV_DISC_REQUEST;
-       if (p2p->cfg->send_action(p2p->cfg->cb_ctx, dev->oper_freq,
-                                 go->p2p_device_addr, p2p->cfg->dev_addr,
-                                 go->p2p_device_addr,
-                                 wpabuf_head(req), wpabuf_len(req), 1000) < 0)
-       {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Failed to send Action frame");
+       wait_time = 1000;
+       if (p2p->cfg->max_listen && wait_time > p2p->cfg->max_listen)
+               wait_time = p2p->cfg->max_listen;
+       if (p2p_send_action(p2p, dev->oper_freq, go->info.p2p_device_addr,
+                           p2p->cfg->dev_addr, go->info.p2p_device_addr,
+                           wpabuf_head(req), wpabuf_len(req), wait_time) < 0) {
+               p2p_dbg(p2p, "Failed to send Action frame");
                wpabuf_free(req);
                /* TODO: how to recover from failure? */
                return -1;
@@ -138,8 +128,7 @@ static struct wpabuf * p2p_build_dev_disc_resp(u8 dialog_token, u8 status)
 
 void p2p_dev_disc_resp_cb(struct p2p_data *p2p, int success)
 {
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Device Discoverability Response TX callback: success=%d",
+       p2p_dbg(p2p, "Device Discoverability Response TX callback: success=%d",
                success);
        p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
 }
@@ -154,18 +143,15 @@ static void p2p_send_dev_disc_resp(struct p2p_data *p2p, u8 dialog_token,
        if (resp == NULL)
                return;
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Sending Device Discoverability Response to " MACSTR
+       p2p_dbg(p2p, "Sending Device Discoverability Response to " MACSTR
                " (status %u freq %d)",
                MAC2STR(addr), status, freq);
 
        p2p->pending_action_state = P2P_PENDING_DEV_DISC_RESPONSE;
-       if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, addr,
-                                 p2p->cfg->dev_addr, p2p->cfg->dev_addr,
-                                 wpabuf_head(resp), wpabuf_len(resp), 200) <
-           0) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Failed to send Action frame");
+       if (p2p_send_action(p2p, freq, addr, p2p->cfg->dev_addr,
+                           p2p->cfg->dev_addr,
+                           wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
+               p2p_dbg(p2p, "Failed to send Action frame");
        }
 
        wpabuf_free(resp);
@@ -178,17 +164,14 @@ void p2p_process_dev_disc_req(struct p2p_data *p2p, const u8 *sa,
        struct p2p_message msg;
        size_t g;
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Received Device Discoverability Request from " MACSTR
+       p2p_dbg(p2p, "Received Device Discoverability Request from " MACSTR
                " (freq=%d)", MAC2STR(sa), rx_freq);
 
        if (p2p_parse(data, len, &msg))
                return;
 
        if (msg.dialog_token == 0) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Invalid Dialog Token 0 (must be nonzero) in "
-                       "Device Discoverability Request");
+               p2p_dbg(p2p, "Invalid Dialog Token 0 (must be nonzero) in Device Discoverability Request");
                p2p_send_dev_disc_resp(p2p, msg.dialog_token, sa, rx_freq,
                                       P2P_SC_FAIL_INVALID_PARAMS);
                p2p_parse_free(&msg);
@@ -196,9 +179,7 @@ void p2p_process_dev_disc_req(struct p2p_data *p2p, const u8 *sa,
        }
 
        if (msg.device_id == NULL) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: P2P Device ID attribute missing from Device "
-                       "Discoverability Request");
+               p2p_dbg(p2p, "P2P Device ID attribute missing from Device Discoverability Request");
                p2p_send_dev_disc_resp(p2p, msg.dialog_token, sa, rx_freq,
                                       P2P_SC_FAIL_INVALID_PARAMS);
                p2p_parse_free(&msg);
@@ -208,9 +189,7 @@ void p2p_process_dev_disc_req(struct p2p_data *p2p, const u8 *sa,
        for (g = 0; g < p2p->num_groups; g++) {
                if (p2p_group_go_discover(p2p->groups[g], msg.device_id, sa,
                                          rx_freq) == 0) {
-                       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Scheduled "
-                               "GO Discoverability Request for the target "
-                               "device");
+                       p2p_dbg(p2p, "Scheduled GO Discoverability Request for the target device");
                        /*
                         * P2P group code will use a callback to indicate TX
                         * status, so that we can reply to the request once the
@@ -225,9 +204,7 @@ void p2p_process_dev_disc_req(struct p2p_data *p2p, const u8 *sa,
                }
        }
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Requested client "
-               "was not found in any group or did not support client "
-               "discoverability");
+       p2p_dbg(p2p, "Requested client was not found in any group or did not support client discoverability");
        p2p_send_dev_disc_resp(p2p, msg.dialog_token, sa, rx_freq,
                               P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE);
        p2p_parse_free(&msg);
@@ -241,14 +218,13 @@ void p2p_process_dev_disc_resp(struct p2p_data *p2p, const u8 *sa,
        struct p2p_device *go;
        u8 status;
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Received Device Discoverability Response from " MACSTR,
+       p2p_dbg(p2p, "Received Device Discoverability Response from " MACSTR,
                MAC2STR(sa));
 
        go = p2p->pending_client_disc_go;
-       if (go == NULL || os_memcmp(sa, go->p2p_device_addr, ETH_ALEN) != 0) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Ignore unexpected "
-                       "Device Discoverability Response");
+       if (go == NULL ||
+           os_memcmp(sa, go->info.p2p_device_addr, ETH_ALEN) != 0) {
+               p2p_dbg(p2p, "Ignore unexpected Device Discoverability Response");
                return;
        }
 
@@ -261,9 +237,7 @@ void p2p_process_dev_disc_resp(struct p2p_data *p2p, const u8 *sa,
        }
 
        if (msg.dialog_token != go->dialog_token) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Ignore Device "
-                       "Discoverability Response with unexpected dialog "
-                       "token %u (expected %u)",
+               p2p_dbg(p2p, "Ignore Device Discoverability Response with unexpected dialog token %u (expected %u)",
                        msg.dialog_token, go->dialog_token);
                p2p_parse_free(&msg);
                return;
@@ -272,17 +246,14 @@ void p2p_process_dev_disc_resp(struct p2p_data *p2p, const u8 *sa,
        status = *msg.status;
        p2p_parse_free(&msg);
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Device Discoverability Response status %u", status);
+       p2p_dbg(p2p, "Device Discoverability Response status %u", status);
 
        if (p2p->go_neg_peer == NULL ||
            os_memcmp(p2p->pending_client_disc_addr,
-                     p2p->go_neg_peer->p2p_device_addr, ETH_ALEN) != 0 ||
-           os_memcmp(p2p->go_neg_peer->member_in_go_dev, go->p2p_device_addr,
-                     ETH_ALEN) != 0) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: No pending "
-                       "operation with the client discoverability peer "
-                       "anymore");
+                     p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN) != 0 ||
+           os_memcmp(p2p->go_neg_peer->member_in_go_dev,
+                     go->info.p2p_device_addr, ETH_ALEN) != 0) {
+               p2p_dbg(p2p, "No pending operation with the client discoverability peer anymore");
                return;
        }
 
@@ -291,8 +262,7 @@ void p2p_process_dev_disc_resp(struct p2p_data *p2p, const u8 *sa,
                 * Peer is expected to be awake for at least 100 TU; try to
                 * connect immediately.
                 */
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Client discoverability request succeeded");
+               p2p_dbg(p2p, "Client discoverability request succeeded");
                if (p2p->state == P2P_CONNECT) {
                        /*
                         * Change state to force the timeout to start in
@@ -308,8 +278,7 @@ void p2p_process_dev_disc_resp(struct p2p_data *p2p, const u8 *sa,
                 * Client discoverability request failed; try to connect from
                 * timeout.
                 */
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Client discoverability request failed");
+               p2p_dbg(p2p, "Client discoverability request failed");
                p2p_set_timeout(p2p, 0, 500000);
        }
 
@@ -318,14 +287,12 @@ void p2p_process_dev_disc_resp(struct p2p_data *p2p, const u8 *sa,
 
 void p2p_go_disc_req_cb(struct p2p_data *p2p, int success)
 {
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: GO Discoverability Request TX callback: success=%d",
+       p2p_dbg(p2p, "GO Discoverability Request TX callback: success=%d",
                success);
        p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
 
        if (p2p->pending_dev_disc_dialog_token == 0) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: No pending Device "
-                       "Discoverability Request");
+               p2p_dbg(p2p, "No pending Device Discoverability Request");
                return;
        }
 
@@ -345,11 +312,9 @@ void p2p_process_go_disc_req(struct p2p_data *p2p, const u8 *da, const u8 *sa,
        unsigned int tu;
        struct wpabuf *ies;
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Received GO Discoverability Request - remain awake for "
-               "100 TU");
+       p2p_dbg(p2p, "Received GO Discoverability Request - remain awake for 100 TU");
 
-       ies = p2p_build_probe_resp_ies(p2p);
+       ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
        if (ies == NULL)
                return;
 
@@ -358,9 +323,7 @@ void p2p_process_go_disc_req(struct p2p_data *p2p, const u8 *da, const u8 *sa,
        tu = 100;
        if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, rx_freq, 1024 * tu / 1000,
                    ies) < 0) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Failed to start listen mode for client "
-                       "discoverability");
+               p2p_dbg(p2p, "Failed to start listen mode for client discoverability");
        }
        wpabuf_free(ies);
 }